PSidhu PSidhu - 1 year ago 75
Python Question

Setting Flask dictionary value via Jinja

If I was to use the following in my Jinja template:

{% set data['enabled'] = True %}

I receive the error
TemplateSyntaxError: expected token '=', got '['
. Setting one word variables is fine, but as the error states, setting dictionary values through Jinja brings an error.

Is there a workaround for this issue? Thanks.

Answer Source

Jinja2 tries to limit assignments in its code, to remove the logic from the view (check out an MVC explanation).

If you still want to do an assignment you will have to use update with a do block. For this you have to enable Expression Statements. After that you can try something like this:

{% do session.update({'logged_in':'True'}) %}