Django sitemap styling
I have several hundred thousand urls, so I would like to cache my sitemaps. This creates a sitemap just fine:
from django.contrib.sitemaps import Sitemap, views as sitemap_views
class StuffSitemap(Sitemap):
limit = 1000
def items(self):
return Stuff.objects.all().order_by('date')
urlpatterns = [
...,
url(r'^sitemap\.xml$', sitemap_views.index, {'sitemaps': {'stuff': StuffSitemap()}}),
url(r'^sitemap-(?P<section>.+)\.xml$', sitemap_views.sitemap, {'sitemaps': {'stuff': StuffSitemap()}}, name='django.contrib.sitemaps.views.sitemap'),
...
]
Decorating the index view sitemap.xml
works fine (even with my own decorators):
from django.views.decorators.cache import cache_control
url(r'^sitemap\.xml$', cache_control(max_age=3600, public=True)(sitemap_views.index), {'sitemaps': {'stuff': StuffSitemap()}}),
But the moment I decorate the actual sitemap view, things start to break:
url(r'^sitemap-(?P<section>.+)\.xml$', cache_control(max_age=3600, public=True)(sitemap_views.sitemap), {'sitemaps': {'stuff': StuffSitemap()}}, name='django.contrib.sitemaps.views.sitemap')
Here's the complete traceback:
Internal Server Error: /sitemap.xml
Traceback (most recent call last):
File "/Users/blender/Projects/project/.venv/lib/python2.7/site-packages/django/core/handlers/base.py", line 104, in get_response
response = middleware_method(request, callback, callback_args, callback_kwargs)
File "/Users/blender/Projects/project/.venv/lib/python2.7/site-packages/debug_toolbar/middleware.py", line 77, in process_view
response = panel.process_view(request, view_func, view_args, view_kwargs)
File "/Users/blender/Projects/project/.venv/lib/python2.7/site-packages/debug_toolbar_line_profiler/panel.py", line 176, in process_view
out = self.profiler.runcall(view_func, *args, **view_kwargs)
File "/usr/local/Cellar/python/2.7.9/Frameworks/Python.framework/Versions/2.7/lib/python2.7/cProfile.py", line 149, in runcall
return func(*args, **kw)
File "/Users/blender/Projects/project/.venv/lib/python2.7/site-packages/django/contrib/sitemaps/views.py", line 17, in inner
response = func(request, *args, **kwargs)
File "/Users/blender/Projects/project/.venv/lib/python2.7/site-packages/django/contrib/sitemaps/views.py", line 37, in index
sitemap_url_name, kwargs={'section': section})
File "/Users/blender/Projects/project/.venv/lib/python2.7/site-packages/django/core/urlresolvers.py", line 551, in reverse
return iri_to_uri(resolver._reverse_with_prefix(view, prefix, *args, **kwargs))
File "/Users/blender/Projects/project/.venv/lib/python2.7/site-packages/django/core/urlresolvers.py", line 468, in _reverse_with_prefix
(lookup_view_s, args, kwargs, len(patterns), patterns))
NoReverseMatch: Reverse for 'django.contrib.sitemaps.views.sitemap' with arguments '()' and keyword arguments '{'section': 'stuff'}' not found. 0 pattern(s) tried: []
Curiously, both of these views are embellished when defined:
def x_robots_tag(func):
@wraps(func)
def inner(request, *args, **kwargs):
response = func(request, *args, **kwargs)
response['X-Robots-Tag'] = 'noindex, noodp, noarchive'
return response
return inner
@x_robots_tag
def index(request, sitemaps,
template_name='sitemap_index.xml', content_type='application/xml',
sitemap_url_name='django.contrib.sitemaps.views.sitemap'):
....
It seems to me that the decorator is preventing the Django graphics editor from handling the decorated sitemap
view correctly , as it uses **kwargs
instead of an explicit section
kwarg:
sitemap_url = urlresolvers.reverse(sitemap_url_name, kwargs={'section': section})
But that doesn't explain why this works, as the views are already decorated. Any thoughts?
source to share
No one has answered this question yet
Check out similar questions: