Django包含标签与可配置的模板 [英] Django inclusion tag with configurable template

查看:122
本文介绍了Django包含标签与可配置的模板的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经创建了一个包含标签,但是我希望能够使模板可选配置。似乎没有支持这个开箱即用,所以我想看看人们这样做 - 也许一个方法首先搜索模板目录中的特定模板名称,然后回到默认模板。 / p>

  @ register.inclusion_tag('foo.html',takes_context = True)
/ pre>

解决方案

inclusion_tag decorator只是一个捷径 - 作为使用特定上下文呈现特定模板的简单方法。一旦你想转移到外面,它就不能再帮助你了。但是,这意味着您必须长时间地编写标签,如文档中所述,并将所需的模板作为参数传递。


I've created an inclusion tag, however I'd like to be able to make the template optionally configurable. There doesn't seem to be support for this out of the box, so I'd like to see how people did this - maybe a method search the templates directory first for a specific template name and then falling back to the default template.

@register.inclusion_tag('foo.html', takes_context=True)

解决方案

The inclusion_tag decorator is just a shortcut - it's meant as a simple way of rendering a specific template with a specific context. As soon as you want to move outside of that, it can no longer help you. But that just means you'll have to write the tag the long way, as explained in the documentation, and pass the template you want as a parameter.

这篇关于Django包含标签与可配置的模板的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆