I am looking for a way to iterate over variable in django template. In template I do not know how to assign variable in for loop.
In get_context_date of ListView i am assigning data of two files to two vars like:
context[f"{t_name}_header"] = db_oper
context[f"{t_name}_body"] = db_header
Context is generated for two files which results with vars like:
context["test1_header"]
context["test1_body"]
context["test2_header"]
context["test2_body"]`
test1 and test2 are names populated from model. I can access them with object_list in template.
In template html file I would like to iterate over context like:
{% for object in object_list %}
{% for item in object.name|add:"_header" %}
...do stuff
{% endfor %}
{% for item in object.name|add:"_body" %}
...do stuff
{% endfor %}
{% endfor %}`
In result in html template I got nothing.
How to properly filter variable in for loop so it could go thru eq. test1_header ?
Maybe you can change your data structure? Using a dict for example:
context['list'] = {}
for t_name in t_names:
context['list'][t_name] = {"header": db_oper, "body": db_header}
And then in your template:
{% for t_name, data in list.items %}
<h2>{{ t_name }}</h2>
{% for item in data.header %}
...do stuff
{% endfor %}
{% for item in data.body %}
...do stuff
{% endfor %}
{% endfor %}