gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 21:38:57 25 4
gpt4 key购买 nike

我有几十万个 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com