I am trying to assign value to a dictionary in Jinja2 and its not working properly and showing error.
expected token 'end of statement block', got '='
My Code:
{% set sequence = ['a1', 'b1']%}
{% set dic = {} %}
{% for filter in search_result.filters %}
{% for seq_key in sequence %}
{% if seq_key == filter.key %}
{# here i wish to create a dictionary where key= seq_key and value = filter_object#}
{% do dic[seq_key]=filter %}
{% endif %}
{% endfor %}
{% endfor %}
The do
statement takes only an expression, not a statement like an assignment. Since Jinja2 does not directly support assigning to a dict by key, you can call the dict's update
method with a singleton dict containing the new key-value mapping instead.
Change:
{% do dic[seq_key]=filter %}
to:
{% do dic.update({seq_key: filter}) %}