使用包含标签装饰器和动态模板

发布于 2024-10-10 22:48:16 字数 260 浏览 0 评论 0原文

这是标准包含标签:

@register.inclusion_tag('results.html')
def show_results(poll):
...

我想知道是否可以使用包含标签来动态定义模板标签。示例:

@register.inclusion_tag('%s.html' % PATH)
def show_results(poll, PATH):
 ...

`

Here is the standard inclusion tag:

@register.inclusion_tag('results.html')
def show_results(poll):
...

I'd like to know if is it possible to use an inclusion tag, defining dynamically the template tag. Example:

@register.inclusion_tag('%s.html' % PATH)
def show_results(poll, PATH):
 ...

`

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

心欲静而疯不止 2024-10-17 22:48:16

不,这是不可能的,因为装饰器(或者实际上是 Python 函数)通常不是这样工作的。首次导入模块时会评估参数。

您需要编写适当的自定义标记,而不使用 inclusion_tag 快捷装饰器。这实际上并不那么难,文档向您展示了如何操作。

No, this isn't possible, because that's not how decorators (or indeed Python functions generally) work. Parameters are evaluated when the module is first imported.

You'd need to write a proper custom tag, without using the inclusion_tag shortcut decorator. This actually isn't all that hard, the documentation shows you how.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文