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

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.

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>

The {% ifchanged %} tag is very useful for adding headers into a rendered html table when the headings need to be changed.

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.

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 %}

<div>
        <fieldset>
            <legend>{{ form_2.name_working.label }}</legend>
            {% for radio in form_2.name_working %}
            <div class="myradio">
                {{ radio }}
            </div>
            {% endfor %}
        </fieldset>
    </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 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 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.)

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?

 <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>

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:

 <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 (ie: if you need it to be rendered once in the inner loop just use if forloop.first).

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?

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.

# a simple structure with nested levels
data = [
('first', ['choice a', 'choice b']),
('second', ['choice c']),
('third', ['choice d', 'choice e', 'choice f']),
]
<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 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.

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 %}

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.)