Python 在Django通用视图中指定不同的模板名称

Python 在Django通用视图中指定不同的模板名称,python,django,django-urls,Python,Django,Django Urls,我在我的url.py中有通用视图的代码 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_in

我在我的url.py中有通用视图的代码

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”模板的通用视图

但是,既然在这个urlpattern中有更多的通用视图,那么我应该如何使用相同的infodict提供不同的模板名称呢?我不想使用太多的infodict,也不能使用默认的模板名

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

谢谢你的帮助

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


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

如果您想为不同的视图提供不同的模板名称,通常的做法是为每个URL模式传递一个唯一的字典。例如:

urlpatterns = patterns('',
    url(r'^home/$', 'my.views.home', {'template_name': 'home.html'}, name='home'),
    url(r'^about/$', 'my.views.about', {'template_name': 'about.html'}, name='about'),
)

这种模式是常见且可接受的。

没有那么简单,但如果有大量不同的模式匹配同一视图,则可能很有用:

base_dict={
...
#defaults go here
}
def make_dict(template_name,template_object_name):
    base_dict.update({
        'template_name':template_name,
        'template_object_name':template_object_name,
    })
    return base_dict

urlpatterns += patterns('django.views.generic.date_based',
(r'^gindex/$', 'archive_index', make_dict('index1.html','latest_poll_list')),
(r'^hindex/$', 'archive_index', make_dict('index2.html','oldest_poll_list')),
)
对于许多类似的泛型视图,这将稍微压缩代码调用,而牺牲一点透明度。如果有许多行自定义相同的几个参数,那么这可能是最容易阅读的


最后,如果您的所有或大部分视图都需要一些(但不是全部)相同的信息,请不要忘记a是多么有用。与上述解决方案相比,设置只需稍微多做一点工作,但它的扩展性要好得多,因为它可以保证这一点(除非使用不带RequestContext关键字的render\u to\u响应快捷方式)无论视图或url配置如何更改,模板都始终可以使用默认值。

您可以定义包装视图函数来参数化常规视图。 在URL.py中添加模式

url(r'^/(?P<tmpl_name>\w+)/$', 'my.views.datebasedproxy')
其中,tmpl_矩阵是一个假设列表,它将模板文件名与参数匹配,而otherparameters代表基于日期的函数所需的其他字典项使用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')),
)

-1这并没有解决这个问题。如果infodict有五个或六个项目,您真的会在单独的字典中复制每个url的所有公共参数吗?我当然不会。为了可读性,我可能会;特别是如果这些论点将来可能会改变的话。我还是更喜欢你的答案。:-)-1“更新”代码不起作用;你试过了吗?dict.update()不会返回更新后的dict,它会“就地”更新dict,但不返回任何内容。另一个解决方案是不必要的冗长,您可以只使用dict(infodict,blah=“blah”)。您是对的。但是,我认为如果有很多模式需要匹配,那么详细的示例可能会更好。但可能没有那么有用。
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')),
)