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 argumentsIs 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.
更多推荐
发布评论