# django.template.exceptions.TemplateSyntaxError: Invalid block tag on line 126: 'set', expected 'empty' or 'endfor'. Did you forget to register or load this tag?

**URL:** <https://forum.djangoproject.com/t/django-template-exceptions-templatesyntaxerror-invalid-block-tag-on-line-126-set-expected-empty-or-endfor-did-you-forget-to-register-or-load-this-tag/29993>\
**Category:** Templates & Frontend\
**Created:** [April 9, 2024, 11:00am UTC](https://forum.djangoproject.com/t/django-template-exceptions-templatesyntaxerror-invalid-block-tag-on-line-126-set-expected-empty-or-endfor-did-you-forget-to-register-or-load-this-tag/29993 "2024-04-09T11:00:22Z")\
**Posts on this page:** 16\
**Page:** 1

<div class="post-metadata">

**Author:** ![salarmander](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/salarmander/32/20490_2.png) [@salarmander](https://forum.djangoproject.com/u/salarmander)\
**Post date:** [April 9, 2024, 11:00am UTC](https://forum.djangoproject.com/t/django-template-exceptions-templatesyntaxerror-invalid-block-tag-on-line-126-set-expected-empty-or-endfor-did-you-forget-to-register-or-load-this-tag/29993/1 "2024-04-09T11:00:23Z")

</div>

i want to use {% set … } but it has problem is django.template.exceptions.TemplateSyntaxError: Invalid block tag on line 126: ‘set’, expected ‘empty’ or ‘endfor’. Did you forget to register or load this tag?

what should i do ?

````auto
                <div class="carousel-inner">
                    {% for knowledge in knowledges %}
                        {% set count = 0 %}
                        {% for photo in photo01s %}
                            {% if photo.category01 == knowledge %}
                                {% set count = count + 1 %}
                                <div class="carousel-item {% if forloop.first %}active{% endif %}">
                                    <div class="row">
                                        <div class="card" style="background-image: url('{{ photo.image01.url }}');"></div>
                                    </div>
                                </div>
                            {% endif %}
                        {% endfor %}
                    {% endfor %}
                </div>```
````

---

<div class="post-metadata">

**Author:** ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)\
**Post date:** [April 9, 2024, 11:24am UTC](https://forum.djangoproject.com/t/django-template-exceptions-templatesyntaxerror-invalid-block-tag-on-line-126-set-expected-empty-or-endfor-did-you-forget-to-register-or-load-this-tag/29993/2 "2024-04-09T11:24:31Z")

</div>

The Django template language does not have a `set` tag.

See [Built-in template tags and filters | Django documentation | Django](https://docs.djangoproject.com/en/5.0/ref/templates/builtins/) for the list of available tags.

Also, in the case of this template you’re showing here, I don’t see why you’re even trying to do this. If you can explain why you’re trying to do this, maybe we can help you identify the appropriate alternative.

---

<div class="post-metadata">

**Author:** ![salarmander](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/salarmander/32/20490_2.png) [@salarmander](https://forum.djangoproject.com/u/salarmander)\
**Post date:** [April 10, 2024, 6:35am UTC](https://forum.djangoproject.com/t/django-template-exceptions-templatesyntaxerror-invalid-block-tag-on-line-126-set-expected-empty-or-endfor-did-you-forget-to-register-or-load-this-tag/29993/3 "2024-04-10T06:35:23Z")

</div>

i want to create a slider and each knowledge in knowledges is a independent carousel of each another. so, for each knowledge i want my carousel-item at least it have 5 cards for each one. But it only has one card in each carousel-item.

---

<div class="post-metadata">

**Author:** ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)\
**Post date:** [April 10, 2024, 12:12pm UTC](https://forum.djangoproject.com/t/django-template-exceptions-templatesyntaxerror-invalid-block-tag-on-line-126-set-expected-empty-or-endfor-did-you-forget-to-register-or-load-this-tag/29993/4 "2024-04-10T12:12:13Z")

</div>

You would build these structures in your view, allowing the template to render them. That type of “data organization” work doesn’t belong in the templates.

---

<div class="post-metadata">

**Author:** ![salarmander](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/salarmander/32/20490_2.png) [@salarmander](https://forum.djangoproject.com/u/salarmander)\
**Post date:** [April 10, 2024, 12:43pm UTC](https://forum.djangoproject.com/t/django-template-exceptions-templatesyntaxerror-invalid-block-tag-on-line-126-set-expected-empty-or-endfor-did-you-forget-to-register-or-load-this-tag/29993/5 "2024-04-10T12:43:59Z")

</div>

> models.py

```auto
class Knowledge(models.Model):
    name01 = models.CharField(max_length=100, null=False, blank=False)

    def __str__ (self):
        return self.name01

class Photo01(models.Model):
    category01 = models.ForeignKey(Knowledge, on_delete=models.SET_NULL, null=True, blank=True)
    image01 = models.ImageField(null=False, blank=False)
    description01 = models.TextField()
    title01 = models.CharField(max_length=100, default='')

    def __str__ (self):
        return self.title01

```

> views.py

```auto
def knowledge01(request):
    knowledges = Knowledge.objects.all()
    photo01s = Photo01.objects.all()

    context = {'knowledges': knowledges, 'photo01s': photo01s}
    return render(request, 'photos/knowledge01.html', context)

```

this is my models and views files. Does it have any problem ?

---

<div class="post-metadata">

**Author:** ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)\
**Post date:** [April 10, 2024, 1:26pm UTC](https://forum.djangoproject.com/t/django-template-exceptions-templatesyntaxerror-invalid-block-tag-on-line-126-set-expected-empty-or-endfor-did-you-forget-to-register-or-load-this-tag/29993/6 "2024-04-10T13:26:44Z")

</div>

Since Your photos are related to the knowledges by the foreign keys field, you don’t need the second query for the photos.

For any given instance of `Knowledge`, the set of related `Photo01` instances can be referenced from that relationship.

Example: If you have an instance of `Knowledge` named `knowledge`, then `knowledge.photo01_set.all()` is the set of `Photo01` related to `knowledge`.

I suggest you review the appropriate docs on [ForeignKey](https://docs.djangoproject.com/en/5.0/ref/models/fields/#module-django.db.models.fields.related) and [Many-to-one relationships](https://docs.djangoproject.com/en/5.0/topics/db/examples/many_to_one/)

---

<div class="post-metadata">

**Author:** ![salarmander](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/salarmander/32/20490_2.png) [@salarmander](https://forum.djangoproject.com/u/salarmander)\
**Post date:** [April 10, 2024, 2:11pm UTC](https://forum.djangoproject.com/t/django-template-exceptions-templatesyntaxerror-invalid-block-tag-on-line-126-set-expected-empty-or-endfor-did-you-forget-to-register-or-load-this-tag/29993/7 "2024-04-10T14:11:56Z")

</div>

thanks for your help. My code work. But, now i’m having a new issue that i want to have 5 items in each carousel-item but it has only one item for each carousel-item. what should i do next ?

> html file

```auto
                <div class="carousel-inner">
                    {% for knowledge in knowledges %}
                        {% for photo in knowledge.photo01_set.all %}
                            <div class="carousel-item {% if forloop.first %}active{% endif %}">
                                     <div class="row">
                                         <div class="card" style="background-image: url('{{ photo.image01.url }}');"></div>
                                     </div>
                                 </div>
                         {% endfor %}
                    {% endfor %}
                </div>  

```

> views.py

```auto
    knowledges = Knowledge.objects.prefetch_related('photo01_set')

    context = {'knowledges': knowledges}
    return render(request, 'photos/knowledge01.html', context)

```

---

<div class="post-metadata">

**Author:** ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)\
**Post date:** [April 10, 2024, 2:17pm UTC](https://forum.djangoproject.com/t/django-template-exceptions-templatesyntaxerror-invalid-block-tag-on-line-126-set-expected-empty-or-endfor-did-you-forget-to-register-or-load-this-tag/29993/8 "2024-04-10T14:17:41Z")

</div>

If you want all of knowledge.photo01\_set.all inside the `carousel-item` div, then the `for` tag needs to be inside the div.

---

<div class="post-metadata">

**Author:** ![salarmander](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/salarmander/32/20490_2.png) [@salarmander](https://forum.djangoproject.com/u/salarmander)\
**Post date:** [April 10, 2024, 2:33pm UTC](https://forum.djangoproject.com/t/django-template-exceptions-templatesyntaxerror-invalid-block-tag-on-line-126-set-expected-empty-or-endfor-did-you-forget-to-register-or-load-this-tag/29993/9 "2024-04-10T14:33:17Z")

</div>

> This is what i want to have.

 ![Screenshot 2024-04-10 213235](https://us1.discourse-cdn.com/flex026/uploads/djangoproject/original/3X/d/c/dc7cac70020423f9f3ad2024d38b5f7531438952.jpeg)

> But result of my code is like this

My idea is that i have 5 cards in the first carousel-item. If i click next button, it will go to the second one and this one also have 5 card. And i want each carousel-item the same like that.

---

<div class="post-metadata">

**Author:** ![salarmander](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/salarmander/32/20490_2.png) [@salarmander](https://forum.djangoproject.com/u/salarmander)\
**Post date:** [April 10, 2024, 2:34pm UTC](https://forum.djangoproject.com/t/django-template-exceptions-templatesyntaxerror-invalid-block-tag-on-line-126-set-expected-empty-or-endfor-did-you-forget-to-register-or-load-this-tag/29993/10 "2024-04-10T14:34:59Z")

</div>

> But result of my code is like this
> 
> ![Screenshot 2024-04-10 213344](https://us1.discourse-cdn.com/flex026/uploads/djangoproject/original/3X/2/a/2aa200fca517c05d01b39e94041efef3bf9c75fe.jpeg)

---

<div class="post-metadata">

**Author:** ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)\
**Post date:** [April 10, 2024, 2:36pm UTC](https://forum.djangoproject.com/t/django-template-exceptions-templatesyntaxerror-invalid-block-tag-on-line-126-set-expected-empty-or-endfor-did-you-forget-to-register-or-load-this-tag/29993/11 "2024-04-10T14:36:20Z")

</div>

Forget the templating for the moment.

What would the HTML look like for what you’re trying to create?

Start from that, then work backwards into the template.

---

<div class="post-metadata">

**Author:** ![salarmander](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/salarmander/32/20490_2.png) [@salarmander](https://forum.djangoproject.com/u/salarmander)\
**Post date:** [April 10, 2024, 2:48pm UTC](https://forum.djangoproject.com/t/django-template-exceptions-templatesyntaxerror-invalid-block-tag-on-line-126-set-expected-empty-or-endfor-did-you-forget-to-register-or-load-this-tag/29993/12 "2024-04-10T14:48:12Z")

</div>

i had created a static html file before i put it into django.

> It like this

```auto
                <div class="carousel-inner">
                    <div class="carousel-item active">
                        <div class="row">
                           <div class="card" style="background-image: url('https://i.pinimg.com/564x/66/f3/83/66f38398faba38139e3b4c541f05c706.jpg');"></div>
                           <div class="card" style="background-image: url('https://i.pinimg.com/564x/66/f3/83/66f38398faba38139e3b4c541f05c706.jpg');"></div>
                           <div class="card" style="background-image: url('https://i.pinimg.com/564x/66/f3/83/66f38398faba38139e3b4c541f05c706.jpg');"></div>
                           <div class="card" style="background-image: url('https://i.pinimg.com/564x/66/f3/83/66f38398faba38139e3b4c541f05c706.jpg');"></div>
                           <div class="card" style="background-image: url('https://i.pinimg.com/564x/66/f3/83/66f38398faba38139e3b4c541f05c706.jpg');"></div>
                        </div>
                    </div>
                    <div class="carousel-item active">
                        <div class="row">
                            <div class="card" style="background-image: url('https://i.pinimg.com/564x/66/f3/83/66f38398faba38139e3b4c541f05c706.jpg');"></div>
                            <div class="card" style="background-image: url('https://i.pinimg.com/564x/66/f3/83/66f38398faba38139e3b4c541f05c706.jpg');"></div>
                            <div class="card" style="background-image: url('https://i.pinimg.com/564x/66/f3/83/66f38398faba38139e3b4c541f05c706.jpg');"></div>
                            <div class="card" style="background-image: url('https://i.pinimg.com/564x/66/f3/83/66f38398faba38139e3b4c541f05c706.jpg');"></div>
                            <div class="card" style="background-image: url('https://i.pinimg.com/564x/66/f3/83/66f38398faba38139e3b4c541f05c706.jpg');"></div>
                        </div>
                    </div>
                </div>

```

And my problem is django code.

---

<div class="post-metadata">

**Author:** ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)\
**Post date:** [April 10, 2024, 2:52pm UTC](https://forum.djangoproject.com/t/django-template-exceptions-templatesyntaxerror-invalid-block-tag-on-line-126-set-expected-empty-or-endfor-did-you-forget-to-register-or-load-this-tag/29993/13 "2024-04-10T14:52:15Z")

</div>

> [@salarmander](#):
>
> ```auto
> <div class="card" style="background-image: url('https://i.pinimg.com/564x/66/f3/83/66f38398faba38139e3b4c541f05c706.jpg');"></div>
> <div class="card" style="background-image: url('https://i.pinimg.com/564x/66/f3/83/66f38398faba38139e3b4c541f05c706.jpg');"></div>
> <div class="card" style="background-image: url('https://i.pinimg.com/564x/66/f3/83/66f38398faba38139e3b4c541f05c706.jpg');"></div>
> <div class="card" style="background-image: url('https://i.pinimg.com/564x/66/f3/83/66f38398faba38139e3b4c541f05c706.jpg');"></div>
> <div class="card" style="background-image: url('https://i.pinimg.com/564x/66/f3/83/66f38398faba38139e3b4c541f05c706.jpg');"></div>
>  
> 
> ```

So it’s in _this_ part where you want to iterate over the individual images. These are the only lines being repeated.

The problem is that you have the `<div class="carousel-item ...` and `<div class="row">` inside your `for` loop for the images, when it needs to be outside that loop.

---

<div class="post-metadata">

**Author:** ![antoinehumbert](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/antoinehumbert/32/14851_2.png) [@antoinehumbert](https://forum.djangoproject.com/u/antoinehumbert)\
**Post date:** [April 10, 2024, 9:16pm UTC](https://forum.djangoproject.com/t/django-template-exceptions-templatesyntaxerror-invalid-block-tag-on-line-126-set-expected-empty-or-endfor-did-you-forget-to-register-or-load-this-tag/29993/14 "2024-04-10T21:16:39Z")

</div>

Hi, as far as I understand, you want to group photos by batches of 5 photos and create a `carousel-item` for each batch.

As already suggested by @KenWhitesell, you should prepare the data to be displayed (the batches of 5 photos each) in your view.

Using the [itertools](https://docs.python.org/3/library/itertools.html#module-itertools) module will help you achieve this, using the [chain.from\_iterable](https://docs.python.org/3/library/itertools.html#itertools.chain.from_iterable) method for iterating on knowleges and photos inside each kowledge, then using [batched](https://docs.python.org/3/library/itertools.html#itertools.batched) function to group photos (depending on your python version, this functionmay not be available but you can still use the equivalent implementation shown in documentation). This would give something like the following:

View:

```auto
import itertools
...

    knowledges = Knowledge.objects.prefetch_related('photo01_set')
    batches = itertools.batched(
   
     itertools.chain.from_iterable(k.photo01_set.all() for k in knowledges),
        n=5
    )
    context = {'batches': batches}
    return render(request, 'photos/knowledge01.html', context)

```

Template:

```auto
                <div class="carousel-inner">
                    {% for batch in batches %}
                            <div class="carousel-item {% if forloop.first %}active{% endif %}">
                                     <div class="row">
                                       {% for photo in batch %}
                                         <div class="card" style="background-image: url('{{ photo.image01.url }}');"></div>
                                     </div>
                                   {% endfor %}
                                 </div>
                    {% endfor %}
                </div>

```

---

<div class="post-metadata">

**Author:** ![salarmander](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/salarmander/32/20490_2.png) [@salarmander](https://forum.djangoproject.com/u/salarmander)\
**Post date:** [April 11, 2024, 3:18pm UTC](https://forum.djangoproject.com/t/django-template-exceptions-templatesyntaxerror-invalid-block-tag-on-line-126-set-expected-empty-or-endfor-did-you-forget-to-register-or-load-this-tag/29993/15 "2024-04-11T15:18:58Z")

</div>

Thanks for your help. I think you guessed right about my idea. Since I’m now a first-year student and busy with my test, I will try your code and give you feedback as soon as possible.

---

<div class="post-metadata">

**Author:** ![salarmander](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/salarmander/32/20490_2.png) [@salarmander](https://forum.djangoproject.com/u/salarmander)\
**Post date:** [April 11, 2024, 3:22pm UTC](https://forum.djangoproject.com/t/django-template-exceptions-templatesyntaxerror-invalid-block-tag-on-line-126-set-expected-empty-or-endfor-did-you-forget-to-register-or-load-this-tag/29993/16 "2024-04-11T15:22:41Z")

</div>

You are very kind to take the time to answer me. This is my demo code in a static HTML file before I integrate it into Django. I repeated it as I want it to resemble a demo and to have an overview of my ideas of what it should look like.
