django包含标签中的位置参数的任意数量?(Arbitrary number of positional arguments in django inclusion tag?)

编程入门 行业动态 更新时间:2024-10-28 17:17:15
django包含标签中的位置参数的任意数量?(Arbitrary number of positional arguments in django inclusion tag?)

我正在尝试编写一个带有任意数量参数的django包含标记:

@register.inclusion_tag('so.html') def table_field(*args): fields = [] for arg in args: fields.append(arg) return { 'fields': fields, }

但是,当我从django的模板引擎中调用它时:

{% table_field form.hr form.bp form.o2_sat %}

我收到错误:

table_field takes 0 arguments

这是django模板引擎的另一个局限性吗?

I'm trying to write a django inclusion tag that takes an arbitrary number of arguments:

@register.inclusion_tag('so.html') def table_field(*args): fields = [] for arg in args: fields.append(arg) return { 'fields': fields, }

However, when I call this from django's template engine:

{% table_field form.hr form.bp form.o2_sat %}

I get the error:

table_field takes 0 arguments

Is this yet another limitation of django's template engine?

最满意答案

从1.2.1开始,您可以将参数传递给包含标签。

这是我的mod到django投票模板标签的一个例子

@register.inclusion_tag("voting/vote_form.html", takes_context=True) def vote_form(context, vote_object, vote_dict, score_dict): if isinstance(vote_dict, dict):

并且模板如下所示:

{% vote_form bookmark vote_dict score_dict %}

我不喜欢这一点是没有办法来命名这些论点,只是为了使它们有序,但它确实有效。

现在对我不那么清楚的是,为什么当你指定了takes_context时,那个父上下文不会与你在返回时用来渲染包含模板的上下文一起传递。

你尝试使用*参数将不会工作,因为通过的#参数是检查功能。

This is another limitation of django's inclusion tags. Currently, in django's trunk version, this cannot be done.

更多推荐

本文发布于:2023-08-06 10:44:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1446743.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:数量   位置   参数   标签   django

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!