Iac*_*cks 2 python django django-urls
我的urls.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),
)
Run Code Online (Sandbox Code Playgroud)
因此,转到地址/ gindex /将使用带有'index.html'模板的通用视图.
但由于我在这个urlpattern中会有更多通用视图,我应该如何使用相同的infodict提供不同的模板名称?我不想使用大量的infodicts,我不能使用默认的模板名称.
请注意,这也适用于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')),
)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2457 次 |
| 最近记录: |