Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check if a variable is defined in django templating language [closed]

I am using django 1.5, I need to check if a variable is defined (and have it not work if the variable is defined but None, 0, "", etc...). Something like:

{% ifexists a_variable %}
    <p> Hey the variable exists </p>
{% endifexists %}

I don't how best to do this...

like image 891
Cucwx Avatar asked Jul 16 '13 08:07

Cucwx


1 Answers

Please read the documentation.

The {% if %} tag evaluates a variable, and if that variable is “true” (i.e. exists ...

{% if athlete_list %}
    Number of athletes: {{ athlete_list|length }}
{% elif athlete_in_locker_room_list %}
    Athletes should be out of the locker room soon!
 {% else %}
    No athletes.
{% endif %}

You might also want to check out the rather handy Django tutorial for writing public views.

like image 169
Sam Nicholls Avatar answered Oct 11 '22 21:10

Sam Nicholls