---
title: '{% if %}'
description: if condition.
date: '2026-08-02'
categories:
  - Template Tag
  - Logic
canonical: https://coolify.djangotemplatetagsandfilters.com/tags/if/
doc_link: https://docs.djangoproject.com/en/6.0/ref/templates/builtins/#if
---

# {% if %}

if condition.

## Documentation

Conditionals in Django templates work just like they do in Python. The syntax is:

```django
{% if some_conditions %}
  Output this block.
{% elif other_conditions %}
  Output this block.
{% else %}
  Output this block.
{% endif %}
```

All of the Python comparison and logical operators are available:

### Comparison Operators

- `==` – Equals.
- `!=` – Doesn’t equal.
- `>` – Is greater than.
- `<` – Is less than.
- `>=` – Is greater than or equal to.
- `<=` – Is less than or equal to.
- `is` – Is the same object.
- `is not` – Is not the same object.
- `in` – Is in a sequence. \*Note that the only types of sequences you can create in the template are lists of strings, which you do by comma-separating the values:

  ```django
  {% if animal in "elephant,giraffe,donkey" %}
  ```

  Other sequences must be made available to the template from the view.

### Logical Operators

- `and` (e.g., `if a and b:`)
- `or` (e.g., `if a or b:`)
- `not` (e.g., `if not a:`)

## Commentary

1. You must include spaces around the comparison operators.

   ```django
   {% if a == b or c > d %}
   ```

   If you fail to include spaces, you will get a `TemplateSyntaxError`.
2. To check for the existence a URL parameter, use:

   ```django
   {% if request.GET.foo %}
   ```

   This will return `True` if `foo` is passed on the querystring and has some value.
