Generate Django sitemap while using site framework - django

I am using sites framework with RequestSite (no SITE_ID set) to generate content based on domain. I need to generate sitemaps for each domain with different results but I didnt find a way how to make this two frameworks work together. Is there any way to get Site of the current request in Sitemap? (getting it from SITE_ID config is not an option for me).
Here is an example of what I would like to do:
from django.contrib.sitemaps import Sitemap
from blog.models import Entry
class BlogSitemap(Sitemap):
def items(self, request):
return Entry.objects.filter(is_draft=False, site=request.site)
But its not possible because there is no request in items(). Is there any other way how to filter items in sitemap based on site?

Try following example:
from django.contrib.sitemaps import Sitemap
from django.contrib.sitemaps.views import sitemap
from blog.models import Entry
class BlogSitemap(Sitemap):
_cached_site = None
def items(self):
return Entry.objects.filter(is_draft=False, site=self._cached_site)
def get_urls(self, page=1, site=None, protocol=None):
self._cached_site = site
return super(BlogSitemap, self).get_urls(page=page, site=site, protocol=protocol)
And in urls.py
urlpatterns = [
url('sitemap.xml', sitemap, {
'sitemaps': {'blog': BlogSitemap}
}, name='django.contrib.sitemaps.views.sitemap'),
# ...
# other your urls
]
This should work now. Let me know if you'll have any questions.

Related

Page not found (404) on django

I am using django 3.2 to build a personal e-commerce project, Got Error-Page not found (404) on visiting my products page url Method:GET URL:http://127.0.0.1:8000/products Using the URLconf defined in main.urls. I have followed all the conventions and the other pages are all working but this one is giving me a[urls[\views][1][browser display] hard time as i feel everything is correct, i might be wrong, pls help and if u need to see more of my code, please let me know.
This is my code for urls
from django.urls import path
from django.views.generic.detail import DetailView
from django.views.generic import *
from main import models, views
app_name = 'main'
urlpatterns = [
path('', views.home, name='home'),
path('about_us/', views.about_us, name='about_us'),
path('contact-us/', views.ContactUsView.as_view(), name='contact_us'),
path(
"products/<slug:tag>/",
views.ProductListView.as_view(),
name="products"
),
path("product/<slug:slug>/", DetailView.as_view(model=models.Product), name='product'),
]
my code for views
from django.views.generic.edit import FormView
from .forms import ContactForm
from django.views.generic.list import ListView
from django.views.generic.detail import DetailView
from django.shortcuts import get_object_or_404
from main import models
class ProductListView(ListView):
template_name = "main/product_list.html"
paginate_by = 4
def get_queryset(self):
tag = self.kwargs['tag']
self.tag = None
if tag != "all":
self.tag = get_object_or_404(
models.ProductTag, slug=tag
)
if self.tag:
products = models.Product.objects.active().filter(
tags=self.tag
)
else:
products = models.Product.objects.active()
return products.order_by("name")
then my browser
Page not found (404)
Request Method: GET
Request URL: http://127.0.0.1:8000/products
Using the URLconf defined in booktime.urls, Django tried these URL patterns, in this order:
admin/
[name='home']
about_us/ [name='about_us']
contact-us/ [name='contact_us']
products/<slug:tag>/ [name='products']
product/<slug:slug>/ [name='product']
^media/(?P<path>.*)$
The current path, products, didn’t match any of these.
You have to create a path to url ending with "products/". You create url just to sent with parameter "tags". "products/slug:tag/"
So, add 'products/' to your urls path the url below and you'll access with 'http://127.0.0.1:8000/products/'
path(
"products/",
views.ProductListView.as_view(),
name="products"
)

How to go from one page to another in django

I'm new to Django and python (infact my first language which I've only been learning for 3 months)
I'm creating this website using Django and I'm not able to go from one page to another using the href tag. it throws at me a 404 error saying "current path didn't match any of these"
This is my code
views.py
from django.shortcuts import render
from django.http import HttpResponseRedirect
from .models import off
# Create your views here.
def homepage(request):
return render(request=request,
template_name='main/home.html',
context={'toll' : off.objects.all})
def secondpage(request):
return render(request = request,
template_name = 'main/next.html')
main/urls.py
from django.urls import path
from . import views
app_name = 'main'
urlpatterns = [
path('',views.homepage,name='homepage'),
path('',views.secondpage,name='secondpage')
]
templates/mains/home.html
<div class="topnav">
Link
Link
Link
</div>
I also request the helper to simply it for me as I wouldn't be able to handle complex and advanced python terminology
Thanks In Advance From a Friend
Arvind
I think you should read this to start with Django.

Django Caching on front-end

I was working with 2 applications that are within a DJango project: "customer" and "vendors". Each application has a HTML file named "testindex.html".
Whenever I typed:
http://myhost/customer/basic_info
the correct page would show up
If I typed
http://myhost/vendors/basic_info
the page from http://myhost/customer/basic_info would show up
I found out that it was due to caching (since both applications use "testindex.html"). So again, "testindex.html" is caching.
How can one get around this problem?
TIA
Details are listed below. I have the following views defined:
urls.py for the project
urlpatterns = [
... snip ...
url(r'^customer/', include('libmstr.customer.urls')),
url(r'^vendors/', include('libmstr.vendors.urls')),
]
views.py for customer
from django.shortcuts import render
def basic_info(request):
return render(request, 'testindex.html', {})
views.py for vendors
from django.shortcuts import render
def basic_info(request):
return render(request, 'testindex.html', {})
urls.py for customers
from django.conf.urls import url
from . import views
# list of templates
app_name = 'customer'
urlpatterns = [
url(r'^basic_info/$', views.basic_info, name='basic_info'),
]
urls.py for vendors
from django.conf.urls import url
from . import views
# list of templates
app_name = 'vendors'
urlpatterns = [
url(r'^basic_info/$', views.basic_info, name='basic_info'),
]
It sounds like you have two templates, customers/templates/testindex.html and vendors/templates/testindex.html.
When you call render(request, 'testindex.html', {}), the app directories template loader searches the templates directory for each app in INSTALLED_APPS, and stops the first time it finds a match. If customers is above vendors in INSTALLED_APPS, then it will always use the customers template.
For this reason, Django recommends that you name your templates customers/templates/customers/testindex.html and vendors/templates/vendors/testindex.html, and change your views to use customers/testindex.html and vendors/testindex.html. This way you avoid clashes.

Recursive URL routing in Django

I want to have a (fairly simple) emulation of SELECT queries through URLs.
For example, in a blogging engine, You'd like /tag/sometag/ to refer to the posts having the sometag tag. Also /tag/sometag/or/tag/other/and/year/2013 should be a valid URL, beside other more complex urls. So, having (theoretically) no limit on the size of the url, I would suggest this should be done recursively, but how could it be handled in Django URL Routing model?
I would use a common URL pattern for all those URLs.
url(r'^query/([\w/]*)/$', 'app.views.view_with_query'),
You would receive all the "tag/sometag/or/tag/other/and/year/2013" as a param for the view.
Then, you can parse the param and extract the info (tag, value, tag, value, year, value) to make the query.
update 2021
django.conf.urls.url is deprecated in version 3.1 and above. Here is the solution for recursive url routing in django:
urls.py
from django.urls import path, re_path
from .views import index
urlpatterns = [
re_path(r'^query/([\w/]*)/$', index, name='index'),
]
views.py
from django.shortcuts import render, HttpResponse
# Create your views here.
def index(request, *args, **kwargs):
print('-----')
print(args)
print(kwargs)
return HttpResponse('<h1>hello world</h1>')
If I call python manage.py run server, and go to 'http://127.0.0.1:8000/query/nice/', I can see these in terminal:
-----
('nice',)
{}
I know this is an old question but for those who reach here in future, starting from django 2.0+, you can use path converter with path to match the path in the url:
# urls.py
urlpatterns = [
path('query/<path:p>/', views.query, name='query'),
]
# views.py
def query(request, p):
# here, p = "tag/sometag/or/tag/other/and/year/2013"
...

Django sitemap index example

I have following models relation:
class Section(models.Model):
section = models.CharField(max_length=200, unique=True)
name = models.CharField(max_length=200, blank = True)
class Article (models.Model):
url = models.CharField(max_length = 30, unique=True)
is_published = models.BooleanField()
section = models.ForeignKey(Section)
I need to create a sitemap for articles, which contains sitemap files for sections. I was reading django documentation about it here http://docs.djangoproject.com/en/dev/ref/contrib/sitemaps/
But didn't manage to find answer how can I:
Define sitemap class in this case
How can I pass section parameters into url file (as it's explained in
the docs)
From where can I get {'sitemaps': sitemaps} if I defined
sitemap as a python class in another file in the application
If i understand correctly you want to use sitemap index that would point at seperate sitemap xml files each for every section.
Django supports this feature by providing a separate sitemap view for index sitemaps.
Haven't used that feature before but something like the following would probably work in your case.
### sitemaps.py
from django.contrib.sitemaps import GenericSitemap
from models import Section
all_sitemaps = {}
for section in Section.objects.all():
info_dict = {
'queryset': section.article_set.filter(is_published=True),
}
sitemap = GenericSitemap(info_dict,priority=0.6)
# dict key is provided as 'section' in sitemap index view
all_sitemaps[section.name] = sitemap
### urls.py
from sitemaps import all_sitemaps as sitemaps
...
...
...
urlpatterns += patterns('',
(r'^sitemap.xml$', 'django.contrib.sitemaps.views.index', {'sitemaps': sitemaps}),
(r'^sitemap-(?P<section>.+)\.xml$', 'django.contrib.sitemaps.views.sitemap', {'sitemaps': sitemaps}),
)
Django==2.2
Python==3.6
Here is the better and easy way to use sitemap index in Django,
install django.contrib.sitemaps in the project by adding it to INSTALLED_APPS of settings.py.
Write sitemaps.py file in your apps and define classes as you need. Example extend django.contrib.sitemap.Sitemap in StaticViewSitemap sitemap class for static URLs, make sure your all static URL has the name for reverse lookup(getting URL from URL name)
# app/sitemap.py
from django.contrib import sitemaps
from django.urls import reverse
class StaticViewSitemap(sitemaps.Sitemap):
priority = 0.6
changefreq = 'monthly'
def items(self):
# URLs names
return ['index', 'aboutus', 'ourstory',]
def location(self, item):
return reverse(item)
Import all sitemaps in urls.py
import sitemap and index from django.contrib.sitemaps.views
then create a dictionary with sitemaps
# urls.py
from django.contrib.sitemaps.views import sitemap, index
from app.sitemaps import StaticViewSitemap
# add as many as sitemap you need as one key
sitemaps = {
"static" : StaticViewSitemap,
}
urlpatterns = [
# sitemap.xml index will have all sitemap-......xmls index
path('sitemap.xml', index, {'sitemaps': sitemaps}, name='django.contrib.sitemaps.views.index'),
# sitemap-<section>.xml here <section> will be replaced by the key from sitemaps dict
path('sitemap-<section>.xml', sitemap, {'sitemaps': sitemaps}, name='django.contrib.sitemaps.views.sitemap'),
]
Here you will have two sitemaps 1. sitemaps.xml 2. sitemaps-static.xml
Run server open URL: http://localhost:8000/sitemap.xml
<sitemapindex xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
<sitemap>
<loc>http://127.0.0.1:8000/sitemap-static.xml</loc>
</sitemap>
</sitemapindex>
Django automatically created an index of sitemaps now open URL: http://127.0.0.1:8000/sitemap-static.xml
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
<url>
<loc>http://localhost:8000/</loc>
<changefreq>monthly</changefreq>
<priority>0.6</priority>
</url>
<url>
<loc>http://localhost:8000/about-us</loc>
<changefreq>monthly</changefreq>
<priority>0.6</priority>
</url>
<url>
<loc>http://localhost:8000/our-story</loc>
<changefreq>monthly</changefreq>
<priority>0.6</priority>
</url>
</urlset>
For me, the accepted answer was impacting development and testing cycle speed since it made python manage.py commands run more slowly -- I had a little more to do in the DB than this example.
Here are the changes I made to mitigate (adapted to the example). Have yet to battle test it but this seems to do the trick (Python3):
### sitemaps.py
class SitemapLookup():
"""
Instantiated class replaces the dictionary of {'sitemap-section': Sitemap} for urls.py
Speeds up application load time by only querying the DB when a sitemap is first requested.
"""
def __init__(self):
self.sitemaps = {}
def __iter__(self):
self._generate_sitemaps_dict()
return self.sitemaps.__iter__()
def __getitem__(self, key):
self._generate_sitemaps_dict()
return self.sitemaps[key]
def items(self):
self._generate_sitemaps_dict()
return self.sitemaps.items()
def _generate_sitemaps_dict(self):
if self.sitemaps:
return
for section in Section.objects.all():
info_dict = {
'queryset': section.article_set.filter(is_published=True),
}
# dict key is provided as 'section' in sitemap index view
self.sitemaps[section.name] = GenericSitemap(info_dict, priority=0.6)
### urls.py
from sitemaps import SitemapLookup
...
...
...
sitemaps = SitemapLookup()
urlpatterns += patterns('',
(r'^sitemap.xml$', 'django.contrib.sitemaps.views.index', {'sitemaps': sitemaps}),
(r'^sitemap-(?P<section>.+)\.xml$', 'django.contrib.sitemaps.views.sitemap', {'sitemaps': sitemaps}),
)