0

I have some code like:

{% if key_var is not none %}
  {{ my_list[key_var] }}
{% endif %}

However, I'm getting an error:

jinja2.exceptions.UndefinedError: 'list object' has no attribute 'None'

Is there a way to get Jinja2 to not run the code if the IF statement is false? Or am I missing something else?

Thank you for your help :)

4
  • Why not {% if key_var %} and leave it at that?
    – dfundako
    Nov 8, 2018 at 18:20
  • That snippet works for me on the latest Jinja2 (2.10). What version of Jinja2 are you using? And are you sure that that fragment is the culprit? Nov 8, 2018 at 18:35
  • @dfundako Because zero would do the wrong thing Nov 8, 2018 at 18:38
  • I'm using Jinja2 2.9.6. I'll see if I can upgrade to 2.10.
    – VFR292
    Nov 9, 2018 at 3:32

1 Answer 1

0
{% if my_list[key_var] is defined %}
  {{ my_list[key_var] }}
{% endif %}

Got the behavior I was looking for. I didn't want my_list[key_var] to run inside of the IF statement when the key_var is None

I don't know why this worked when my original attempt didn't. If anyone knows, please fill me in.

Thank you!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.