【问题标题】:Specifying different template names in Django generic views在 Django 通用视图中指定不同的模板名称
【发布时间】:2010-10-15 05:39:12
【问题描述】:

我的 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),
)

所以转到地址 /gindex/ 将使用带有 'index.html' 模板的通用视图。

但是由于我将在此 urlpattern 中拥有更多通用视图,我应该如何使用相同的 infodict 提供不同的模板名称?我不想使用大量的 infodict,也不能使用默认的模板名称。

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

感谢您的帮助!

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

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

【问题讨论】:

    标签: python django django-urls


    【解决方案1】:

    如果您想为不同的视图提供不同的模板名称,通常的做法确实是将唯一的字典传递给每个 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'),
    )
    

    这种模式很常见,也可以接受。

    【讨论】:

    • -1 这没有解决问题。如果 infodict 有五个或六个项目,您真的会在单独的字典中复制每个 url 中的所有公共参数吗?我当然不会。
    • 为了可读性,我可能会;特别是如果这些论点将来可能会发生变化。
    • 我还是更喜欢你的回答。 :-)
    【解决方案2】:

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

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

    对于许多类似的通用视图,这会稍微压缩代码调用,但会牺牲一点透明度。如果您有很多行自定义相同的几个参数,这可能最容易阅读。

    最后,如果您的所有或大部分视图都需要一些(但不是全部)相同的信息,请永远不要忘记context processor 的用处。设置只需要比上述解决方案多一点工作,但它扩展得更好,因为它将保证(除非您使用没有 RequestContext 关键字的 render_to_response 快捷方式)默认值将始终可用于您的模板无论如何您的视图或 url 配置更改。

    【讨论】:

    • -1 “更新”代码不起作用;你试过吗? dict.update() 不返回更新的字典,它“就地”更新字典并返回无。另一个解决方案是不必要的冗长,您可以只使用 dict(infodict, blah="blah")。
    • 你是对的。但是,我认为如果您有很多模式要匹配,那么详细的示例可能会更好。但可能没那么有用。
    【解决方案3】:

    您可以定义包装视图函数来参数化通用视图。 在你的 urls.py 添加模式

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

    在你的views.py中添加视图函数

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

    其中 tmpl_matrix 是将模板文件名与参数匹配的假设列表,而 otherparameters 代表基于 date_based 函数所需的其他字典项

    【讨论】:

      【解决方案4】:

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2014-03-11
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-07-06
        • 2018-04-13
        • 2011-04-22
        • 1970-01-01
        相关资源
        最近更新 更多