Reputation: 2840
{% 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
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