动态静态页面的Django站点Map

gkn4icbw  于 2023-03-04  发布在  Go
关注(0)|答案(1)|浏览(113)

我的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常量中定义的参数生成此路径的站点Map?我所看到的示例通常是查询数据库以获取详细视图列表。

3htmauhk

3htmauhk1#

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})

相关问题