The Django templating system was never intended to be a full-featured programming language, so it doesn't allow us to execute Python statements in templates (again, see the Philosophy and Limitations section for more on this). But comparing the values of two variables and displaying some results is an all-too-common need, so Django provides the {% ifequal %} tag for us to use.
The {% ifequal %} tag compares two values and displays all values in {% ifequal %} and {% endifequal %} when they are equal.
The following example compares two template variables, user and currentuser.
{% ifequal user currentuser %} <h1>Welcome!</h1> {% endifequal %}
Arguments can be hard-coded strings, casually elicited in single or double quotes, so all of the following code is correct:
{% ifequal section 'sitenews' %} <h1>Site News</h1> {% endifequal %} {% ifequal section "community" %} <h1>Community</h1> {% endifequal %}
Similar to {% if %}, {% ifequal %} supports the optional {% else%} tag:
{% ifequal section 'sitenews' %} <h1>Site News</h1> {% else %} <h1>No News Here</h1> {% endifequal %}
Only template variables, strings, integers and decimals are allowed as arguments to the {% ifequal %} tag. The following are examples of legal parameters:
{% ifequal variable 1 %} {% ifequal variable 1.23 %} {% ifequal variable 'foo' %} {% ifequal variable "foo" %}
Any other types, such as Python's dictionary types, list types, and boolean types, cannot be used in {% ifequal %}. Here are some examples of errors:
{% ifequal variable True %} {% ifequal variable [1, 2, 3] %} {% ifequal variable {'key': 'value'} %}
If you need to determine whether a variable is true or false, use {% if %} instead of {% ifequal %}.