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...
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.
来源:https://stackoverflow.com/questions/17671458/check-if-a-variable-is-defined-in-django-templating-language