---
title: '{% ifchanged %}'
description: checks if value in loop has changed since last iteration.
date: '2026-08-02'
categories:
  - Template Tag
  - Logic
canonical: https://www.djangotemplatetagsandfilters.com/tags/ifchanged/
doc_link: https://docs.djangoproject.com/en/6.0/ref/templates/builtins/#ifchanged
---

# {% ifchanged %}

checks if value in loop has changed since last iteration.

## Documentation

Used within a loop to output something if one or more values has changed since the last loop iteration.

### Variable

```django
foods = [
    {'name': 'Apple', 'category': 'Fruit'},
    {'name': 'Banana', 'category': 'Fruit'},
    {'name': 'Grape', 'category': 'Fruit'},
    {'name': 'Hamburger', 'category': 'Meat'},
    {'name': 'Pepper', 'category': 'Vegetable'},
    {'name': 'Corn', 'category': 'Vegetable'}
]
```

### Template

```django
{% for food in foods %}
  {% ifchanged food.category %}<h3>{{ food.category }}</h3>{% endifchanged %}
  <p>{{ food.name }}</p>
{% endfor %}
```

### Result

```django
<h3>Fruit</h3>
<p>Apple</p>
<p>Banana</p>
<p>Grape</p>

<h3>Meat</h3>
<p>Hamburger</p>

<h3>Vegetable</h3>
<p>Pepper</p>
<p>Corn</p>
```

Notice that the `h3` elements only get inserted if the category has changed since the previous iteration.
