Django Tutorial · Django Syntax

Django If Else

Learn all about Django If Else in this comprehensive tutorial.

5 min read intermediate
  • An if statement evaluates a variable and executes a block of code if the value is true.
  • The elif keyword says "if the previous conditions were not true, then try this condition".
  • The else keyword catches anything which isn't caught by the preceding conditions.
  • The above examples uses the == operator, which is used to check if a variable is equal to a value, but there are many other operators you can use, or you can even drop the operator if you just want to check if a variable is not empty:
  • Is equal to.
  • Is not equal to.
  • Is less than, or equal to.
  • Is greater than.
  • Is greater than, or equal to.
  • To check if more than one condition is true.
  • To check if one of the conditions is true.
  • Combine and and or.
  • To check if a certain item is present in an object.
  • To check if a certain item is not present in an object.
  • Check if two objects are the same.
  • To check if two objects are not the same.

If Statement

An if statement evaluates a variable and executes a block of code if the value is true.

Elif

The elif keyword says "if the previous conditions were not true, then try this condition".

Else

The else keyword catches anything which isn't caught by the preceding conditions.

Operators

The above examples uses the == operator, which is used to check if a variable is equal to a value, but there are many other operators you can use, or you can even drop the operator if you just want to check if a variable is not empty:

==

Is equal to.

!=

Is not equal to.

Is less than.

<=

Is less than, or equal to.

>

Is greater than.

>=

Is greater than, or equal to.

and

To check if more than one condition is true.

or

To check if one of the conditions is true.

and/or

Combine and and or.

Parentheses are not allowed in if statements in Django, so when you combine and and or operators, it is important to know that parentheses are added for and but not for or.

Meaning that the above example is read by the interpreter like this:

in

To check if a certain item is present in an object.

not in

To check if a certain item is not present in an object.

is

Check if two objects are the same.

This operator is different from the == operator, because the == operator checks the values of two objects, but the is operator checks the identity of two objects.

In the view we have two objects, x and y, with the same values:

The two objects have the same value, but is it the same object?

Let us try the same example with the == operator instead:

How can two objects be the same? Well, if you have two objects that points to the same object, then the is operator evaluates to true:

We will demonstrate this by using the {% with %} tag, which allows us to create variables in the template:

is not

To check if two objects are not the same.

Module quiz

2 questions
1

Which of the following is true about Django If Else?

2

What is the most common pitfall when working with Django If Else?

Answer all questions to submit.