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

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

问题描述

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

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)

推荐答案

inclusion_tag 装饰器只是一种快捷方式——它是一种简单的方式来呈现具有特定上下文的特定模板.一旦你想摆脱它,它就不再能帮助你了.但这只是意味着您必须按照文档中的说明长时间编写标签,并将所需的模板作为参数传递.

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天全站免登陆