# Django break statement not allowed in block tags

**URL:** <https://forum.djangoproject.com/t/django-break-statement-not-allowed-in-block-tags/21757>\
**Category:** Mystery Errors\
**Created:** [June 21, 2023, 2:12pm UTC](https://forum.djangoproject.com/t/django-break-statement-not-allowed-in-block-tags/21757 "2023-06-21T14:12:19Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Rachamv](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/rachamv/32/13162_2.png) [@Rachamv](https://forum.djangoproject.com/u/Rachamv)\
**Post date:** [June 21, 2023, 2:12pm UTC](https://forum.djangoproject.com/t/django-break-statement-not-allowed-in-block-tags/21757/1 "2023-06-21T14:12:19Z")

</div>

Error message: “django.template.exceptions.TemplateSyntaxError: Invalid block tag on line 82: ‘break’, expected ‘elif’, ‘else’ or ‘endif’. Did you forget to register or load this tag?”

This error is being caused because I am trying to use the `break` statement in a block tag in a Django template.

To fix this error, I replace it with a `return` statement from the block tag. Which will terminate the `if` statement immediately. But the error remains the same.

My goal is to stop the for loops after listing 5 items from the category, Please I need help to fix this issue  
Here is the code:

```auto
<div class="card">
                <h5 class="mb-30">By Categories</h5>
                <div class="categories-dropdown-wrap font-heading">
                  <ul>
                    {% for c in categories %}
                      <li>
                        <input
                          data-filter="category"
                          class="form-check-input filter-checkbox"
                          type="checkbox"
                          name="checkbox"
                          id="exampleCheckbox2"
                          value="{{ c.id }}"
                        />
                        &nbsp;&nbsp;
                        <a href="{% url 'core:category-product-list' c.cid %}">
                          <img src="{{c.image.url}}" alt="" />{{ c.title }}</a
                        >
                      </li>
                      {% if c.loop.index == 5 %}
                        <li>
                          <p>This is the 5th category.</p>
                        </li>
                        {% break %}
                      {% endif %}
                    {% endfor %}
                  </ul>
                </div>
              </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:** [June 21, 2023, 2:15pm UTC](https://forum.djangoproject.com/t/django-break-statement-not-allowed-in-block-tags/21757/2 "2023-06-21T14:15:41Z")

</div>

There is no `break` tag. (See [Built-in template tags and filters | Django documentation | Django](https://docs.djangoproject.com/en/4.2/ref/templates/builtins/#built-in-template-tags-and-filters))

If you want to limit your iterations, then trim the number of entries being added in the context. (Apply the limit in the view, not the template.)

---

<div class="post-metadata">

**Author:** ![Rachamv](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/rachamv/32/13162_2.png) [@Rachamv](https://forum.djangoproject.com/u/Rachamv)\
**Post date:** [June 21, 2023, 2:20pm UTC](https://forum.djangoproject.com/t/django-break-statement-not-allowed-in-block-tags/21757/3 "2023-06-21T14:20:25Z")

</div>

I only what to limit it to a few places, not the entire function for example the category list on the sidebar can have only 5 while the category list page has the full list.  
How can I achieve that

---

<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:** [June 21, 2023, 2:21pm UTC](https://forum.djangoproject.com/t/django-break-statement-not-allowed-in-block-tags/21757/4 "2023-06-21T14:21:38Z")

</div>

See the [`slice`](https://docs.djangoproject.com/en/4.2/ref/templates/builtins/#slice) filter.

---

<div class="post-metadata">

**Author:** ![Rachamv](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/rachamv/32/13162_2.png) [@Rachamv](https://forum.djangoproject.com/u/Rachamv)\
**Post date:** [June 21, 2023, 2:35pm UTC](https://forum.djangoproject.com/t/django-break-statement-not-allowed-in-block-tags/21757/5 "2023-06-21T14:35:47Z")

</div>

Thanks its solve

```auto
{% for c in categories|slice:":5" %}
                      <li>
                        <input
                          data-filter="category"
                          class="form-check-input filter-checkbox"
                          type="checkbox"
                          name="checkbox"
                          id="exampleCheckbox2"
                          value="{{ c.id }}"
                        />
                        &nbsp;&nbsp;
                        <a href="{% url 'core:category-product-list' c.cid %}">
                          <img src="{{c.image.url}}" alt="" />{{ c.title }}</a
                        >
                      </li>
                    {% endfor %}

```
