Django sitemap for dynamic static pages

huangapple go评论103阅读模式
英文:

Django sitemap for dynamic static pages

问题

我在我的views.py文件中有以下内容:

  1. ACCEPTED = ["x", "y", "z", ...]
  2. def index(request, param):
  3. if not (param in ACCEPTED):
  4. raise Http404
  5. return render(request, "index.html", {"param": param})

URL 地址相当简单:

  1. path('articles/<str:param>/', views.index, name='index'),

我如何为这个路径生成网站地图,仅包括在ACCEPTED常量中定义的已接受参数?通常我看到的示例会查询数据库以获取详细视图的列表。

英文:

I have the following in my views.py:

  1. ACCEPTED = [&quot;x&quot;, &quot;y&quot;, &quot;z&quot;, ...]
  2. def index(request, param):
  3. if not (param in ACCEPTED):
  4. raise Http404
  5. return render(request, &quot;index.html&quot;, {&quot;param&quot;: param})

Url is simple enough:

  1. path(&#39;articles/&lt;str:param&gt;/&#39;, views.index, name=&#39;index&#39;),

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

对于您的页面,可以像这样做:

  1. class StaticViewSitemap(sitemaps.Sitemap):
  2. priority = 0.5
  3. changefreq = 'daily'
  4. def items(self):
  5. return ['x', 'y', 'z', ...]
  6. def location(self, item):
  7. 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:

  1. class StaticViewSitemap(sitemaps.Sitemap):
  2. priority = 0.5
  3. changefreq = &#39;daily&#39;
  4. def items(self):
  5. return [&#39;x&#39;, &#39;y&#39;, &#39;z&#39;, ...]
  6. def location(self, item):
  7. return reverse(index, kwargs={&quot;param&quot;: item})

huangapple
  • 本文由 发表于 2023年2月27日 17:43:53
  • 转载请务必保留本文链接:https://go.coder-hub.com/75578852.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定