How to check if django template variable is defined?
You need to switch your {% block %}
and your {% if %}
{% block messages %}
{% if message %}<div class='imp_message'>{{ message }}</div>{% endif %}
{% endblock %}
To check, in an if statement, you need to compare the value to None
, like this:
{% if some_missing_var is None %}
// code here if some_missing_var exists
{% else %}
// code here if some_missing_var does not exist
{% endif %}
In other cases (from the docs):
Generally, if a variable doesn’t exist, the template system inserts the value of the engine’s
string_if_invalid
configuration option, which is set to''
(the empty string) by default.
I tried some of the other answers, and they didn't work until I read the docs on how invalid variables are handled and the above was made clear.