Django sitemap index example - django

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}),
)

Related

Generate Django sitemap while using site framework

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.

Django Oscar change URL pattern

I have setup a django-oscar project and I'm trying to configure the URLs. My goal is to change /catalogue to /catalog.
As per the documentation I've added app.py in myproject/app.py
myproject/app.py
from django.conf.urls import url, include
from oscar import app
class MyShop(app.Shop):
# Override get_urls method
def get_urls(self):
urlpatterns = [
url(r'^catalog/', include(self.catalogue_app.urls)),
# all the remaining URLs, removed for simplicity
# ...
]
return urlpatterns
application = MyShop()
myproject/urls.py
from django.conf.urls import url, include
from django.contrib import admin
from . import views
from .app import application
urlpatterns = [
url(r'^i18n/', include('django.conf.urls.i18n')),
url(r'^admin/', admin.site.urls),
url(r'', application.urls),
url(r'^index/$',views.index, name = 'index'),
]
The project server runs without any error, but when I try localhost:8000/catalog I get
NoReverseMatch at /catalog/ 'customer' is not a registered namespace.
The expected output is localhost:8000/catalog should return the catalogue page.
You can try this
in app.py
from django.conf.urls import url, include
from oscar import app
class MyShop(app.Shop):
# Override get_urls method
def get_urls(self):
urls = [
url(r'^catalog/', include(self.catalogue_app.urls)),
# all the remaining URLs, removed for simplicity
# ...
]
urls = urls + super(MyShop,self).get_urls()
return urls
application = MyShop()
And in your urls.py
you can simply add this
from myproject.app import application as shop
url(r'', shop.urls),
Hope it help for you
Expanding on c.grey's answer to specify how to replace instead of add the urls -
from django.conf.urls import url, include
from oscar import app
class MyShop(app.Shop):
def get_urls(self):
urls = super(MyShop, self).get_urls()
for index, u in enumerate(urls):
if u.regex.pattern == r'^catalogue/':
urls[index] = url(r'^catalog/', include(self.catalogue_app.urls))
break
return urls
application = MyShop()
You need to include the URLs, not reference them directly.
url(r'', include('application.urls')),

Mezzanine ignores the view, displays the template but does not pass the context

I created a view for my model, with the corresponding urls and template files. Then, in the admin panel, I have created a Rich text page, specifying the same URL (ingredients) defined in urlpatterns. Mezzanine ignores the view, displays the template but does not pass the context.
How can I solve it?
These are the codes:
models.py
from django.db import models
from mezzanine.pages.models import Page
from django.utils.translation import ugettext_lazy as _
class Ingredient(Page):
name = models.CharField(max_length=60)
information = models.TextField(null=True, blank=True, verbose_name=_("Description"))
views.py
from django.template.response import TemplateResponse
from .models import Ingredient
def ingredients(request):
ingredients = Ingredient.objects.all().order_by('name')
templates = ["pages/ingredients.html"]
return TemplateResponse(request, templates, {'ingredients':ingredients})
urls.py
from django.conf.urls import url
from .views import ingredients
urlpatterns = [
url("^$", ingredients, name="ingredients"),
]
TemplateResponse does not expect the request in its arguments. See the docs.
return TemplateResponse(templates, {'ingredients':ingredients})
However I expect you meant to use the standard render function there:
return render(request, "pages/ingredients.html", {'ingredients':ingredients})
Ok, the solution has been define my app urls before any other definition in my project urls.py file.
project_name/project_name/urls.py
# Add the urlpatterns for any custom Django applications here.
# You can also change the ``home`` view to add your own functionality
# to the project's homepage.
urlpatterns = [
url(r'^ingredients/', include("apps.ingredients.urls")),
]
urlpatterns += i18n_patterns(
# Change the admin prefix here to use an alternate URL for the
# admin interface, which would be marginally more secure.
url("^admin/", include(admin.site.urls)),
)

sitemap.xml empty for static urls in Django

I have a Django project and I am trying to create a sitemap for my static urls (no Models). However, when running python manage.py runserver and going to http://127.0.0.1:8000/sitemap.xml, I get it empty:
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"></urlset>
My code looks like this:
#urls.py
from main_app.sitemaps import StaticSitemap
sitemaps = {
'static': StaticSitemap(),
}
urlpatterns = [
...
url(r'^sitemap\.xml$', sitemap, {'sitemaps': sitemaps}),
...
]
urlpatterns += i18n_patterns(
...
)
and
#sitemaps.py
from django.contrib.sitemaps import Sitemap
from django.urls import reverse
class StaticSitemap(Sitemap):
priority = 0.5
changefreq = 'weekly'
i18n = True
def location(self, item):
return reverse(item)
The documentation shows an example with Models and it modifies the function items; but since I do not have, I am not sure if I am missing something there.
What am I doing wrong?
I just found out what the problem was.
It seems that you need to define the function items anyway.
def items(self):
list_of_url_names = ['home', 'about', ..., 'contact']
return list_of_url_names
and then sitemap.xml is not empty anymore.

Django does not match urls

I've been working on a django web app (using Django 1.3), a web catalogue of products, and it was working fine until I was asked to add a custom admin site. I'm fully aware of the Django admin site, but the client is very old-fashioned, and is tech-ignorant so I have to make a "for dummies" version admin site. The root urlconf:
from django.conf.urls.defaults import *
from django.views.generic import TemplateView
from store.models import Category
from django.contrib import admin
admin.autodiscover()
urlpatterns = patterns('',
url(r'^admin/', include(admin.site.urls)),
url(r'^^$', TemplateView.as_view(
template_name='homepage.html',
get_context_data=lambda: {
'crumb': 'home',
'category_list':Category.objects.all()
}
),
name='home'),
url(r'^favicon\.ico$', 'django.views.generic.simple.redirect_to', {'url': '/static/img/favicon.ico'}),
url(r'^store/', include('store.urls', app_name='store', namespace='store')),
)
And the urlconf for the store app:
from django.conf import settings
from django.conf.urls.defaults import *
from store import views
urlpatterns = patterns ('',
url(r'^category/$', views.get_brands, name='get_brands'),
url(r'^(\w+)/$', views.GalleryView.as_view(), name='gallery'),
url(r'^(\w+)/(\w+)/$', views.GalleryView.as_view(), name='gallery'),
)
and the original views:
from django.http import Http404
from django.shortcuts import render, get_object_or_404
from django.views.generic import ListView
from store.models import Category, Brand, Product
def get_brands(request):
q = request.GET.get('q')
if q is not None:
category = get_object_or_404(Category, slug__iexact=q)
try:
brands = category.brands.all()
except:
brands = []
template = 'infobox.html'
data = {
'category': category,
'brands': brands,
}
return render( request, template, data )
class GalleryView(ListView):
context_object_name = 'product_list'
template_name = 'store/gallery.html'
def get_queryset(self):
self.category = get_object_or_404(Category, slug__iexact=self.args[0])
try:
brand = Brand.objects.get(slug__iexact = self.args[1])
self.brand_name = brand.name
except:
#no brand is specified, show products with no brand
if self.category.category_products.filter(brand__isnull=True):
#if there are products with no brand, return those
return self.category.category_products.filter(brand__isnull=True)
else:
#if all products have a brand, return the products of the first brand
all = self.category.brands.all()
if all:
brand = all[0]
self.brand_name = brand.name
return brand.brand_products.all()
else:
raise Http404
else:
#brand is specified, show its products
return Product.objects.filter(category=self.category, brand=brand)
def get_context_data(self, **kwargs):
context = super(GalleryView, self).get_context_data(**kwargs)
category = self.category
category_brands = self.category.brands.all()
context['category_list'] = Category.objects.all()
context['category'] = category
context['crumb'] = category.name
context['category_brands'] = category_brands
try:
context['brand'] = self.brand_name
except:
context['brand'] = None
return context
Now, my custom admin app was working fine on my local dev environment, but when I added the new urls and views to prod, Django doesn't seem to match any of the new urls. The original views and urls still work, but none of the new urls get matched and I just keep getting a 404 Not Found error.
The updated urlconf:
from django.conf import settings
from django.conf.urls.defaults import patterns, include, url
from django.contrib.auth.decorators import login_required
from store import views
admin_urls = patterns ('',
url(r'^$', login_required(views.AdminIndexView.as_view()), name='admin_index'),
url(r'^add/(\w+)/$', login_required(views.AdminAddView.as_view()), name='admin_add'),
)
urlpatterns = patterns ('',
url(r'^category/$', views.get_brands, name='get_brands'),
url(r'^(\w+)/$', views.GalleryView.as_view(), name='gallery'),
url(r'^(\w+)/(\w+)/$', views.GalleryView.as_view(), name='gallery'),
url(r'^login/$', views.admin_login, name='login'),
url(r'^logout/$', views.admin_logout, name='logout'),
url(r'^logout/success/$', views.admin_logout_success, name='logout_success'),
url(r'^test/', views.test, name='test'),
url(r'^admin/', include(admin_urls, namespace='admin')),
url(r'^ajax/$', views.ajax_request, name='ajax_request'),
)
Note that not even the simple '/store/test/' url does not get matched. I'm not really sure why Django isn't matching my urls, and any help is appreciated.
I'm not exactly sure what was happening since all of my templates linked to other pages using the {% url %} tag, but I think the reason my urls were getting muddled up was because I was using the r'^(\w+)/$' regex, so it was matching any word. I simply moved the urlconfs with the (\w+) rule to the bottom of urls.py, and refactored them to be a little more specific and they were gold again.
The updated urlconf:
from django.conf import settings
from django.conf.urls.defaults import patterns, include, url
from django.contrib.auth.decorators import login_required
from store import views
admin_urls = patterns ('',
)
urlpatterns = patterns ('',
url(r'^category/$', views.get_brands, name='get_brands'),
url(r'^(\w+)/$', views.GalleryView.as_view(), name='gallery'),
url(r'^(\w+)/(\w+)/$', views.GalleryView.as_view(), name='gallery'),
url(r'^login/$', views.admin_login, name='login'),
url(r'^logout/$', views.admin_logout, name='logout'),
url(r'^logout/success/$', views.admin_logout_success, name='logout_success'),
url(r'^ajax/$', views.ajax_request, name='ajax_request'),
url(r'^administration/$', login_required(views.AdminIndexView.as_view()), name='admin_index'),
url(r'^administration/add/(\w+)/$', login_required(views.AdminAddView.as_view()), name='admin_add'),
)
As you mentioned in the comments, removing login_required fixes the problem. Here's what the django docs have to say about the decorator:
login_required() does the following:
If the user isn’t logged in, redirect to settings.LOGIN_URL, passing the current absolute path in the query string. Example:
/accounts/login/?next=/polls/3/.
If the user is logged in, execute the view normally
[...] Note that if you don't specify the login_url parameter, you'll
need to map the appropriate Django view to settings.LOGIN_URL.
In other words, it goes to a default url you can override in settings.LOGIN_URL.
Now here's what I think happened - I think you defined your own login here:
url(r'^login/$', views.admin_login, name='login'),
And because the login_required in the new urls pointed at the default url, which doesn't exist it returned 404. However, when you configured the login_required views after your other urls:
url(r'^login/$', views.admin_login, name='login'),
...
url(r'^administration/$', login_required(views.AdminIndexView.as_view()), name='admin_index'),
url(r'^administration/add/(\w+)/$', login_required(views.AdminAddView.as_view()), name='admin_add'),
it worked. Why is that? You didn't overrid LOGIN_URL here right? But you sort of did - because at this point, you redefined the namespace 'login' to point to your own view. Now this isn't mentioned anywhere in the docs, but it makes sense, and looking at the default admin templates you can see this is the namespace being used.
Does that make sense to you?