user2990084
user2990084

Reputation: 2840

UndefinedError in jinja condition

{% if user.photo is defined %} 
{{ user.photo }}
{% else %}
#gravatar
{% endif %} 

I am trying this code, but I am getting UndefinedError: 'user' is undefined in first line.

Everything works fine if i use this:

{% if user is defined %} 
{{ user.photo }}
{% else %}
#gravatar
{% endif %} 

What is wrong with my first code?

Upvotes: 0

Views: 126

Answers (1)

Martijn Pieters
Martijn Pieters

Reputation: 1122342

Your user object isn't set, so test if both are present:

{% if user and user.photo %} 
    {{ user.photo }}
{% else %}
    #gravatar
{% endif %} 

Just testing for boolean truthfulness should be enough.

Upvotes: 2

Related Questions