Django sitemap for dynamic static pages

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

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 = [&quot;x&quot;, &quot;y&quot;, &quot;z&quot;, ...]

def index(request, param):
    if not (param in ACCEPTED):
        raise Http404
    return render(request, &quot;index.html&quot;, {&quot;param&quot;: param})

Url is simple enough:

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

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

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 = &#39;daily&#39;

    def items(self):
        return [&#39;x&#39;, &#39;y&#39;, &#39;z&#39;, ...]

    def location(self, item):
        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:

确定