# How can I display a table in a template with additional category/section rows?

**URL:** <https://forum.djangoproject.com/t/how-can-i-display-a-table-in-a-template-with-additional-category-section-rows/41655>\
**Category:** Templates & Frontend\
**Created:** [July 2, 2025, 3:18pm UTC](https://forum.djangoproject.com/t/how-can-i-display-a-table-in-a-template-with-additional-category-section-rows/41655 "2025-07-02T15:18:42Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![k8004kv](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/k8004kv/32/12148_2.png) [@k8004kv](https://forum.djangoproject.com/u/k8004kv)\
**Post date:** [July 2, 2025, 3:18pm UTC](https://forum.djangoproject.com/t/how-can-i-display-a-table-in-a-template-with-additional-category-section-rows/41655/1 "2025-07-02T15:18:42Z")

</div>

Have a nice day!

I have a non-standard table.  
Which I plan to display in the template.

I load the table into the template from the model.  
And I have additional sections - additional fields.  
These additional fields characterize - Category, Section, Topics.  
I am thinking how to display such a table from the model in the template - as shown in the picture.

To additionally implement additional fields - categories or sections in the table.

I have a rough idea -

I have to create a transit model for exactly such a table - in which the number of rows is equal to the number of rows of the original table + plus the number of rows of category sections in addition.  
That is, there can be about 25 rows of the original table and + plus 6 rows - category sections.  
In total, the required table will have 31 rows of rows.

 ![2025-07-02_17-58-48](https://us1.discourse-cdn.com/flex026/uploads/djangoproject/original/3X/1/b/1b15916ad4c4186892147d13f23fa61dcd0b9969.png)

But how to display them in the form as in my picture - separating categories sections?

**models.py**

```
class CreateNewGPR (models.Model):
    name_object = models.IntegerField(verbose_name="")
    name_category = models.CharField(verbose_name="")
    name_working = models.CharField(verbose_name="")
    type_izm = models.CharField(choices=TYPE_IZMERENIYA, verbose_name="")
    value_work = models.FloatField(verbose_name="")
    lyudi_norma = models.IntegerField(verbose_name="")
    technik_norma = models.IntegerField(verbose_name="")
    date_begin = models.DateField(verbose_name="")
    date_end = models.DateField(verbose_name="")

```

**views.py**

```
filter_qs = CreateNewGPR.objects.filter(name_object=pk)
filter_qs = filter_qs.values("name_working", "type_izm", "value_work", "lyudi_norma", "technik_norma", "date_begin", "date_end")

context['books'] = filter_qs

```

**template.html**

```
<div>
        <table class="pure-table">
            <thead>
                <tr>
                    <th>Name Column 1</th>
                    <th>Name Column 2</th>
                    <th>Name Column 3</th>
                    <th>Name Column 4</th>
                    <th>Name Column 5</th>
                    <th>Name Column 6</th>
                    <th>Name Column 7</th>
                </tr>
            </thead>
            <tbody>
                {% for book in books %}
                <tr>
                    <td>{{ book.name_working }}</td>
                    <td>{{ book.type_izm }}</td>
                    <td>{{ book.value_work }}</td>
                    <td>{{ book.lyudi_norma }}</td>
                    <td>{{ book.technik_norma }}</td>
                    <td>{{ book.date_begin }}</td>
                    <td>{{ book.date_end }}</td>
                </tr>
                {% endfor %}
            </tbody>
        </table>
    </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:** [July 2, 2025, 4:18pm UTC](https://forum.djangoproject.com/t/how-can-i-display-a-table-in-a-template-with-additional-category-section-rows/41655/2 "2025-07-02T16:18:35Z")

</div>

The [`{% ifchanged %} tag`](https://docs.djangoproject.com/en/5.2/ref/templates/builtins/#ifchanged) is very useful for adding headers into a rendered html table when the headings need to be changed.

---

<div class="post-metadata">

**Author:** ![k8004kv](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/k8004kv/32/12148_2.png) [@k8004kv](https://forum.djangoproject.com/u/k8004kv)\
**Post date:** [July 8, 2025, 9:03am UTC](https://forum.djangoproject.com/t/how-can-i-display-a-table-in-a-template-with-additional-category-section-rows/41655/3 "2025-07-08T09:03:38Z")

</div>

How can I pass the values ​​of list elements by index to the template - as the number of iterations of the loop?

I have a loop in the template and I would like to pass the number of iterations in the loop - as the value of the element in the list.

[10, 5, 8] - each element of the list is the required number of iterations.

How can I pass the values ​​of list elements by index to the template - as the number of iterations of the loop?

I have a loop in the template and I would like to pass the number of iterations in the loop - as the value of the element in the list.

[10, 5, 8] - each element of the list is the required number of iterations.

```auto
list_2 = [10, 5, 8]
       j = 0
       i = 0
       {% for len(list_1) ...number of iterations... in %}
            <a>list_1[j]</a>
            {% for list_2[j] ...number of iterations... in %}
            <div>
                {{ list_3[i] }}
                i = i + 1    
            </div>
            {% endfor %}
            j = j + 1
       {% endfor %}

```

* * *

```auto
<div>
        <fieldset>
            <legend>{{ form_2.name_working.label }}</legend>
            {% for radio in form_2.name_working %}
            <div class="myradio">
                {{ radio }}
            </div>
            {% endfor %}
        </fieldset>
    </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:** [July 8, 2025, 12:58pm UTC](https://forum.djangoproject.com/t/how-can-i-display-a-table-in-a-template-with-additional-category-section-rows/41655/4 "2025-07-08T12:58:03Z")

</div>

My gut reaction to this is that you’re trying to do work in the template that should more appropriately be done in the view.

If I were faced with a situation like this, I’d take a step back and re-evaluate how I’m creating my context and look to organize it in a manner more consistent with how templates are intended to work.

For example, if you’ve got a list that has 23 elements, but you only want to render the first 10, then you can construct the context with just the 10 elements to be rendered - or you can use the [`slice`](https://docs.djangoproject.com/en/5.2/ref/templates/builtins/#slice) filter to slice the list within the template.

(More detailed suggestions would require complete information regarding the view and the data being rendered.)

Having said that, see [The Django template language | Django documentation | Django](https://docs.djangoproject.com/en/5.2/ref/templates/language/#variables) for accessing list elements by numeric index. (Briefly, you use the same “dot notation” as you do for accessing dictionary entries or object attribute references.)

---

<div class="post-metadata">

**Author:** ![k8004kv](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/k8004kv/32/12148_2.png) [@k8004kv](https://forum.djangoproject.com/u/k8004kv)\
**Post date:** [July 8, 2025, 3:13pm UTC](https://forum.djangoproject.com/t/how-can-i-display-a-table-in-a-template-with-additional-category-section-rows/41655/5 "2025-07-08T15:13:11Z")

</div>

How can I move the iterable value (radio) from the outer loop to the inner one, bypassing the inner loop iteration?

How can I get the value (radio) of the outer loop. I have a nested loop that generates additional fields. The value from the outer loop. I plan to get the value from the outer loop not 30 times - but how many times in the outer loop 5 times. How can I skip the inner iteration?

```auto
 <fieldset>
    <legend>{{ form_2.name_working.label }}</legend>
    {% for radio in form_2.name_working %}
        {% for i in kol_vo_iteracii_categ|get_range %}
        <a>Cat</a>
        <div class="myradio">
            {{ radio }}
        </div>
        {% endfor %}
    {% endfor %}
</fieldset>

```

- [django](https://stackoverflow.com/questions/tagged/django)
- [templates](https://stackoverflow.com/questions/tagged/templates)

---

<div class="post-metadata">

**Author:** ![sevdog](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/sevdog/32/20692_2.png) [@sevdog](https://forum.djangoproject.com/u/sevdog)\
**Post date:** [July 9, 2025, 7:46am UTC](https://forum.djangoproject.com/t/how-can-i-display-a-table-in-a-template-with-additional-category-section-rows/41655/6 "2025-07-09T07:46:18Z")

</div>

HI @k8004kv, what kind of HTML do you whish to be generated? Can you provide an example?

If you just want the radio element to be rendered once-for-outer-loop, just put it outside the inner loop, like this:

```auto
 <fieldset>
    <legend>{{ form_2.name_working.label }}</legend>
    {% for radio in form_2.name_working %}
        <a>Cat</a>
        <div class="myradio">
            {{ radio }}
        </div>
        {% for i in kol_vo_iteracii_categ|get_range %}
          {# whatever you need to do with inner loop #}
        {% endfor %}
    {% endfor %}
</fieldset>

```

Also remember that you can build complex logic in template loops by [using forloop variables](https://docs.djangoproject.com/en/5.2/ref/templates/builtins/#for) (ie: if you need it to be rendered once in the inner loop just use `if forloop.first`).

---

<div class="post-metadata">

**Author:** ![k8004kv](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/k8004kv/32/12148_2.png) [@k8004kv](https://forum.djangoproject.com/u/k8004kv)\
**Post date:** [July 9, 2025, 8:53am UTC](https://forum.djangoproject.com/t/how-can-i-display-a-table-in-a-template-with-additional-category-section-rows/41655/7 "2025-07-09T08:53:07Z")

</div>

Thank you very much for your help!  
I have been thinking for several days how to make such a scheme.  
I have a choices field.  
I am trying to insert section names between them.  
I can just have 25 choices elements.  
And I want to divide them into groups. For example, into several groups and implement names for each group.  
Something similar to a table with categories.

In the example in the figure,  
from 1 to 11 these are choices elements and between them  
the name of the group by which they are divided.

Moreover, each time the number of choices elements is different, as is the grouping.

How can I insert additional text elements through a loop into the set of choices form elements in the template?

 ![2025-07-02_17-58-48](https://us1.discourse-cdn.com/flex026/uploads/djangoproject/original/3X/1/b/1b15916ad4c4186892147d13f23fa61dcd0b9969.png)

---

<div class="post-metadata">

**Author:** ![sevdog](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/sevdog/32/20692_2.png) [@sevdog](https://forum.djangoproject.com/u/sevdog)\
**Post date:** [July 9, 2025, 9:38am UTC](https://forum.djangoproject.com/t/how-can-i-display-a-table-in-a-template-with-additional-category-section-rows/41655/8 "2025-07-09T09:38:08Z")

</div>

To provide a solution you should also share your model and form configuration, thus I can reason about it.

I can provide you with a sample template snippet to build a table-with-categories, like in your screenshot.

```python
# a simple structure with nested levels
data = [
('first', ['choice a', 'choice b']),
('second', ['choice c']),
('third', ['choice d', 'choice e', 'choice f']),
]

```

```html
<table>
{% for category, values in data %}
  {# render the category in outer loop #}
  <tr>
    <th colspan="4">{{category}}</th>
  </tr>
{% for value in values %}
  {# render value in the inner loop #}
  <tr>
    <td>{{value}}</td>
    <td></td>
    <td></td>
    <td></td>
  </tr>
{% endfor %}
{% endfor %}
</table>

```

However keep in mind that if you have a field with [choices](https://docs.djangoproject.com/en/5.2/ref/models/fields/#choices) there is a specific format which is already implemented in django to handle grouping in `<select>` components.

Everything depends on how you want them to be rendered.

---

<div class="post-metadata">

**Author:** ![k8004kv](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/k8004kv/32/12148_2.png) [@k8004kv](https://forum.djangoproject.com/u/k8004kv)\
**Post date:** [July 9, 2025, 11:04am UTC](https://forum.djangoproject.com/t/how-can-i-display-a-table-in-a-template-with-additional-category-section-rows/41655/9 "2025-07-09T11:04:29Z")

</div>

How can I create a set of elements to place in views?

I have a list of elements.

zzz = [m1-x, m2-x] - the number of repetitions of each group name - the number of elements to group  
xxx = [m1,m1, m1, m1, m2, m2] - all group names that must belong to the choices elements.  
sss = [q, w, e, r, t, y] - the choices elements

vvv = [(‘q’, 'q), (‘w’, ‘w’), (‘e’, ‘e’),(‘r’, 'r), (‘t’, ‘t’), (‘y’, ‘y’)]

[– m1-- {{{ (‘q’, 'q), (‘w’, ‘w’), (‘e’, ‘e’),(‘r’, 'r) }}} , --m2-- {{{ (‘t’, ‘t’), (‘y’, ‘y’) }}}]  
This is roughly how I would like to group

but the lists will always be different - dynamic.

I took this from chatgpt.  
but I don’t know yet how to create such a set of elements.

* * *

…

> GROUPED\_CHOICES = [  
> (‘Fruits’, [  
> (‘apple’, ‘Apple’),  
> (‘banana’, ‘Banana’),  
> ]),  
> (‘Vegetables’, [  
> (‘carrot’, ‘Carrot’),  
> (‘lettuce’, ‘Lettuce’),  
> ]),  
> ]
> 
> …
> 
> from django import forms
> 
> class FoodForm(forms.Form):  
> food = forms.ChoiceField(  
> choices=GROUPED\_CHOICES,  
> widget=forms.RadioSelect  
> )

> {% for group\_label, group\_choices in form.food.field.choices %}  
>   
> {{ group\_label }}  
> {% for choice\_value, choice\_label in group\_choices %}  
> {# Get the radio input manually using the field’s id\_for\_label logic #}  
> {% with field\_id=form.food.auto\_id %}  
> {% set widget\_id = field\_id|string + ‘\_’ + choice\_value %}  
> {% endwith %}  
>   
> \<input type=“radio” name=“{{ form.food.name }}”  
> value=“{{ choice\_value }}”  
> id=“{{ widget\_id }}”  
> {% if form.food.value == choice\_value %}checked{% endif %}\>  
> {{ choice\_label }}  
>   
>   
> {% endfor %}  
>   
> {% endfor %}

---

<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:** [July 9, 2025, 11:12am UTC](https://forum.djangoproject.com/t/how-can-i-display-a-table-in-a-template-with-additional-category-section-rows/41655/10 "2025-07-09T11:12:38Z")

</div>

> [@k8004kv](#):
>
> I have a list of elements.
> 
> [m1-x, m2-x] - the number of repetitions of each group name - the number of elements to group  
> [m1,m1, m1, m1, m2, m2] - all group names that must belong to the choices elements.  
> [q, w, e, r, t, y] - the choices elements

It is not clear what it is you’re trying to create here.

Given the information you’ve shown, what are you looking to generate?  
(What do you mean that “the group names that must belong to the choices elements”?)

Does your expression `[m1,m1, m1, m1, m2, m2]` mean that the previous expression (`[m1-x, m2-x]`) would be `[4, 2]` in this case?

It would probably be most helpful if you used a real example from the (possibly simplified) real data involved. Show enough of a subset of the data to be able to demonstrate what the desired outputs are. (And include the models that correspond to the data.)
