从模板标签返回多个值后,在一个with语句中分配多个变量 [英] Assign multiple variables in a with statement after returning multiple values from a templatetag

查看:128
本文介绍了从模板标签返回多个值后,在一个with语句中分配多个变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法在django模板中的with语句中分配多个变量。我想从一个模板标签返回多个值后,在一个with语句中分配多个变量。



我的用例是这样的:

  {%with a,b,c = object | get_abc%} 
{{a}}
{{b}}
{{c}}
{%endwith%}


解决方案

我不知道这是允许的,但是从文档允许多重分配。



但是,您可以将这3个变量分配给1个变量,这将使其元组对象,您可以轻松地通过其索引迭代。

  {%with var = object | get_abc%} 
{{var.0}}
{{var.1}}
{{var.2}}
{%endwith%}


Is there a way to assign multiple variables in a with statement in a django template. I'd like to assign multiple variables in a with statement after returning multiple values from a templatetag

My use case is this:

{% with a,b,c=object|get_abc %}
    {{a}}
    {{b}}
    {{c}}
{% endwith %} 

解决方案

I'm not sure that this as allowed, however from docs multiple assign is allowed.

But you can assign these 3 variables to 1 variable, which will make it tuple object, which you can easily iterate by its index.

{% with var=object|get_abc %}
  {{ var.0 }}
  {{ var.1 }}
  {{ var.2 }}
{% endwith %}

这篇关于从模板标签返回多个值后,在一个with语句中分配多个变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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