gpt4 book ai didi

python - 在 Django 通用 View 中指定不同的模板名称

转载 作者:行者123 更新时间:2023-11-28 20:12:11 25 4
gpt4 key购买 nike

我的 urls.py 中有通用 View 的代码;

infodict = {
'queryset': Post.objects.all(),
'date_field': 'date',
'template_name': 'index.html',
'template_object_name': 'latest_post_list',
}

urlpatterns += patterns('django.views.generic.date_based',
(r'^gindex/$', 'archive_index', infodict),
)

因此,转到地址/gindex/将使用带有“index.html”模板的通用 View 。

但是由于我将在这个 urlpattern 中有更多通用 View ,我应该如何使用相同的 infodict 提供不同的模板名称?我不想使用大量信息词典,而且我不能使用默认模板名称。

请注意,这也适用于 infodict 中的模板对象名称。

感谢您的帮助!

编辑:这是我关于 stackoverflow 的第一个问题,我对彻底的答案感到惊讶!我更喜欢使用我不知道的 dict 构造函数。我发现使用 python 文档有点困难,因为我通常找不到我要找的东西!

再次感谢所有的答案和不同的方法。

最佳答案

使用 dict() 构造函数:

infodict = {
'queryset': Post.objects.all(),
'date_field': 'date',
'template_name': 'index.html',
'template_object_name': 'latest_post_list',
}

urlpatterns = patterns('django.views.generic.date_based',
url(r'^gindex/$', 'archive_index', dict(infodict, template_name='gindex.html')),
url(r'^hindex/$', 'archive_index', dict(infodict, template_name='hindex.html')),
)

关于python - 在 Django 通用 View 中指定不同的模板名称,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/677793/

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