简体   繁体   English

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

[英]Specifying different template names in Django generic views

I have the code in my urls.py for my generic views;我的 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),
)

So going to the address /gindex/ will use a generic view with the template of 'index.html'.因此,转到地址 /gindex/ 将使用带有“index.html”模板的通用视图。

But since I will have more generic views in this urlpattern, how am I supposed to provide a different template name using the same infodict?但是由于我将在这个 urlpattern 中有更多通用视图,我应该如何使用相同的 infodict 提供不同的模板名称? I don't want to have to use lots of infodicts, and I can't use the default template name.我不想使用大量的信息字典,也不能使用默认的模板名称。

Please note this also applies to template object name within infodict.请注意,这也适用于 infodict 中的模板 object 名称。

Thanks for your help!谢谢你的帮助!

Edit : This is one of my first questions on stackoverflow and I am amazed with the thorough answers.编辑:这是我在 stackoverflow 上的第一个问题,我对彻底的答案感到惊讶。 I prefer using the dict constructor which I didn't know about.我更喜欢使用我不知道的 dict 构造函数。 I find using the python documentation a bit harder as I can't find what i'm looking for usually!我发现使用 python 文档有点困难,因为我通常找不到我要找的东西!

Thanks again for all the answers and different approaches.再次感谢所有答案和不同的方法。

Use the dict() constructor:使用 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')),
)

If you want to supply different template names to different views, the common practice is indeed to pass in a unique dictionary to each URL pattern.如果您想为不同的视图提供不同的模板名称,通常的做法确实是将一个唯一的字典传递给每个 URL 模式。 For example:例如:

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'),
)

This kind of pattern is common and acceptable.这种模式很常见并且可以接受。

Not as simple, but possibly useful if you have a whole lot of different patterns matching the same view:不是那么简单,但如果您有很多不同的模式匹配同一个视图,则可能很有用:

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')),
)

For a lot of similar generic views, this will condense your code calls a wee bit, at the expense of a little bit of transparency.对于许多类似的通用视图,这会稍微压缩你的代码调用,但会牺牲一点透明度。 If you've got many lines customizing the same few parameters, this might be easiest to read.如果您有很多行自定义相同的几个参数,这可能最容易阅读。

Finally, if all or most of your views require some, but not all, of the same information, never forget how useful a context processor is.最后,如果您的所有或大部分视图都需要一些(但不是全部)相同的信息,请永远不要忘记上下文处理器是多么有用。 It takes only a little more work to set up than the above solutions, but it extends much better, because it will guarantee that (unless you use the render_to_response shortcut without the RequestContext keyword) the defaults will always be available to your template no matter how your view or url conf changes.与上述解决方案相比,设置只需要多一点工作,但它的扩展性要好得多,因为它将保证(除非您使用不带 RequestContext 关键字的 render_to_response 快捷方式)无论如何,您的模板始终可以使用默认值您的视图或 url conf 更改。

you can define wrapper view functions to parametrize generic views.您可以定义包装视图函数来参数化通用视图。 In your urls.py add pattern在你的 urls.py 添加模式

url(r'^/(?P<tmpl_name>\w+)/$', 'my.views.datebasedproxy')

in your views.py add view function在你的views.py中添加视图function

def datebasedproxy(request, tmpl_name):
    return django.views.generic.date_based(request,otherparameters,
    template_name=tmpl_matrix[tmpl_name])

where tmpl_matrix is hypothetic list that matches template file name with parameter and otherparameters stands for other dictionary items required for date_based function其中 tmpl_matrix 是将模板文件名与参数匹配的假设列表,而 otherparameters 代表基于 date_based function 所需的其他字典项

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM