我有几十万个网址,所以我想缓存我的网站地图。这样可以很好地生成站点地图:
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'),
...
]
装饰sitemap.xml
索引视图工作正常(即使是我自己的装饰器):
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()}}),
但是当我装饰实际的站点地图视图时,事情就开始破坏了:
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')
这是完整的追溯:
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: []
奇怪的是,这两个视图在定义时都会被修饰:
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'):
....
在我看来,装饰器阻止Django url映射器正确处理装饰的sitemap
视图,因为它使用**kwargs
而不是显式的section
kwarg:
sitemap_url = urlresolvers.reverse(sitemap_url_name, kwargs={'section': section})
但这并没有解释为什么它开始工作,因为视图已经装饰好了。有什么想法吗?
答案 0 :(得分:0)
我通过创建视图函数并返回内置的 django 站点地图视图来缓存我的站点地图:
from django.views.decorators.cache import cache_page
from django.contrib.sitemaps.views import sitemap
@cache_page(60*60*1)
def sitemap_view(request):
return sitemap(request, sitemaps)
urlpatterns = [
path('sitemap.xml', sitemap_view, name='django.contrib.sitemaps.views.sitemap')
]