python - 在 Django 中装饰站点地图 View

标签 python django decorator sitemap

我有几十万个 URL,所以我想缓存我的站点地图。这会生成站点地图就好了:

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索引 View 工作正常(即使使用我自己的装饰器):
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()}}),

但是当我装饰实际的站点地图 View 时,事情就开始崩溃了:
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: []

奇怪的是,这两个 View 在定义时都会被修饰:
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夸格:
sitemap_url = urlresolvers.reverse(sitemap_url_name, kwargs={'section': section})

但这并不能解释为什么它一开始就可以工作,因为 View 已经被装饰了。有什么想法吗?

最佳答案

我通过创建 View 函数并返回内置的 django 站点地图 View 来缓存我的站点地图:

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')
]

关于python - 在 Django 中装饰站点地图 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29719090/

相关文章:

python - 如何用 Sympy 绘制点?

python - NLTK 提取文本中存在的类别并映射到分类法

django - 瞬时使用 Django 模型实例

python - 如何通过装饰器获取底层函数参数信息?

python - 如何使用 PyCharm 观察变量?

python - Django RateLimit 403 页面未被替换

python - Django - 对同一模型使用多个外键

c# - 最佳外部 REST API 访问模式?

python - 包装一个函数隐藏它的属性?

python - 如何用另一个列表中的字典值填充一个列表中的字典值?