How to Use if statement in Django template

Django is a popular web framework for building web applications using the Python programming language. One of the key features of Django is its template system, which allows developers to separate the presentation of a web page from its underlying logic. In this post, we will explore how to use if statements in Django templates to control which parts of a template are displayed to the user.

Basic Syntax

Django templates use the {% if %} and {% endif %} tags to create if statements. The syntax for a basic if statement is as follows:

{% if variable %}
<!-- code to be executed if variable is true -->
{% endif %}

The variable inside the if statement can be any valid template variable, such as a variable passed from the view or a variable calculated within the template.

Example of a simple Django template if statement

One popular, yet simple example of using an if statement in a Django template is when you want to check if the current user is logged in to the website or not, and then provide the necessary steps based on that authentication status. Here is how it is implemented:

<h1>Welcome to my site</h1>

{% if user.is_authenticated %}
    <p>Hello, {{ user.username }}</p>
    <p><a href="{% url 'logout' %}">Logout</a></p>
{% else %}
    <p>You are not logged in.</p>
    <p><a href="{% url 'login' %}">Login</a></p>
{% endif %}

In this example, the if statement is checking the user.is_authenticated attribute to determine if the user is logged in or not. Depending on the result, the template will display different content. If the user is logged in, it will display “Hello, [username]” and a logout link, otherwise it will display “You are not logged in.” and a login link.

This example also includes the {% else %} tag which we will cover in the next section.

Advanced Usage

{% else %} and {% elif %}

In addition to the basic if statement, Django templates also support the use of {% else %} and {% elif %} tags.

The {% else %} tag can be used to specify a block of code to be executed if the condition in the if statement is not met, the way we did in the previous example, while the {% elif %} tag can be used to specify additional conditions to be checked.

But if you want to chain multiple if statements together using the {% elif %} tag to check additional conditions. For example:

{% if variable == 5 %}
    <p>The variable is equal to 5.</p>
{% elif variable == 10 %}
    <p>The variable is equal to 10.</p>
{% else %}
    <p>The variable is not equal to 5 or 10.</p>
{% endif %}

Logical Operators

You can also use logical operators like and ,or and not in Django template if statements if you want to combine multiple conditions together and evaluate the overall truth of the statement. We will look at each of these logical operators one by one:

and:

You can use the and operator if you want to check if all of the conditions in a statement are true. Here’s an example:

{% if user.is_authenticated and user.is_staff %}
    <a href="{% url 'admin:index' %}">Admin Panel</a>
{% endif %}

In this example, the if statement checks if the user is both authenticated and a staff member. If both conditions are true, the template will display a link to the admin panel.

or:

The or operator is used to check if at least one of the conditions in a statement is true.

{% if user.is_authenticated or user.is_staff %}
    <a href="{% url 'my_view' %}">My View</a>
{% endif %}

In this example, the if statement checks if the user is either authenticated or a staff member. If either condition is true, the template will display a link to a specific view.

not:

You can also use not operator to check for the opposite of a condition, for example:

{% if not user.is_authenticated %}
    <a href="{% url 'login' %}">Please Login</a>
{% endif %}

The if statement checks if the user is not authenticated. If the user is not authenticated, the template will display a link to the login page.

It’s worth noting that the and, or and not operators are used to evaluate the truthiness of the conditions, so you can use them not only with booleans but also with any other variable that can be evaluated as true or false.

Comparison Operators

Comparison operators are used to compare the values of two operands and evaluate the truth of a statement.

{% if variable == 5 %}
    <p>The variable is equal to 5.</p>
{% else %}
    <p>The variable is not equal to 5.</p>
{% endif %}

In the above example, we are checking if a variable is ‘equal’ to 5.

You can use the rest of the comparison operators, which I listed below, in the same way as the example.

  • ==: Checks if the value of two operands are equal or not, if yes then condition becomes true. It’s the one we used in the example.
  • !=: Checks if the value of two operands are equal or not, if values are not equal then condition becomes true.
  • >: Checks if the value of left operand is greater than the value of right operand, if yes then condition becomes true. < does the opposite.
  • >=: Checks if the value of left operand is greater than or equal to the value of right operand, if yes then condition becomes true. <= does the opposite.
  • in: Used to check if a value is present in a list or not.
  • not in: Used to check if a value is not present in a list or not.
  • is: Used to check if two variables refer to the same object or not.
  • is not: used to check if two variables refer to the different objects or not.

Best Practices

When using if statements in Django templates, it’s crucial to keep templates simple and easy to read. You can achieve that by utilizing template inheritance. Avoid using too many if statements in a single template, as this can make the template difficult to understand and maintain.

Performance is also an important consideration when using if statements in templates. Avoid using complex logic or calculations in if statements, as this can slow down the rendering of the template.

Conclusion

In this post, we’ve explored how to use if statements in Django templates to control which parts of a template are displayed to the user. We’ve looked at the basic syntax of if statements and advanced usage with the {% else %} and {% elif %} tags. We’ve also discussed best practices for using if statements in templates to ensure good performance and maintainability.

Stephen Mclin
Stephen Mclin

Hey, I'm Steve; I write about Python and Django as if I'm teaching myself. CodingGear is sort of like my learning notes, but for all of us. Hope you'll love the content!

Articles: 90

Leave a Reply

Your email address will not be published. Required fields are marked *