英文:
Django sitemap for dynamic static pages
问题
我在我的views.py文件中有以下内容:
ACCEPTED = ["x", "y", "z", ...]
def index(request, param):
if not (param in ACCEPTED):
raise Http404
return render(request, "index.html", {"param": param})
URL 地址相当简单:
path('articles/<str:param>/', views.index, name='index'),
我如何为这个路径生成网站地图,仅包括在ACCEPTED
常量中定义的已接受参数?通常我看到的示例会查询数据库以获取详细视图的列表。
英文:
I have the following in my views.py:
ACCEPTED = ["x", "y", "z", ...]
def index(request, param):
if not (param in ACCEPTED):
raise Http404
return render(request, "index.html", {"param": param})
Url is simple enough:
path('articles/<str:param>/', views.index, name='index'),
How do I generate a sitemap for this path only for the accepted params defined in the ACCEPTED
constant? Usually examples I've seen query the database for a list of detail views.
答案1
得分: 2
以下是已翻译的部分:
有关您的情况,Django 文档中提供了解决方案:https://docs.djangoproject.com/en/4.1/ref/contrib/sitemaps/#sitemap-for-static-views
对于您的页面,可以像这样做:
class StaticViewSitemap(sitemaps.Sitemap):
priority = 0.5
changefreq = 'daily'
def items(self):
return ['x', 'y', 'z', ...]
def location(self, item):
return reverse(index, kwargs={"param": item})
英文:
There are solutions in the Django documentation for your case: https://docs.djangoproject.com/en/4.1/ref/contrib/sitemaps/#sitemap-for-static-views
For your pages, do something like that:
class StaticViewSitemap(sitemaps.Sitemap):
priority = 0.5
changefreq = 'daily'
def items(self):
return ['x', 'y', 'z', ...]
def location(self, item):
return reverse(index, kwargs={"param": item})
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论