Assign multiple variables in a with statement after returning multiple values ​​from templatetag

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

My use case:

{% with a,b,c=object|get_abc %}
    {{a}}
    {{b}}
    {{c}}
{% endwith %} 
+3
source share
3 answers

I do not think this is possible without a custom templatetag.

However, if your method always returns the same length, you can make it more compact, like this:

{% with a=var.0 b=var.1 c=var.2 %}
  ...
{% endwith %}
+2
source

I'm not sure if this is allowed, however several assignments are allowed from docs .

3 1 , tuple, .

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

Django, , , docs:

Django - Python, HTML. : ,

, Python , , :

{# assuming get_abc returns a dict #}
{% with var=object|get_abc %}
  {{ var.key_a }}
  {{ var.key_b }}
  {{ var.key_c }}
{% endwith %}
0

Source: https://habr.com/ru/post/1676947/


All Articles