In my Django template I am trying to create a for loop within a for loop. I am trying to use the result of one of the fields as the name for the list in the second for constructor.
I have two objects successfully passed to my template:
sectList = {"Id": 1, "Section": "Home", "Identifier": "homeList"}
homeList = {"Id": 1, "Title": "Home", "Url": "home/",
"Id": 2, "Title": "My Account", "Url": "account/",
"Id": 3, "Title": "Full List", "Url": "fullList/",}
In my template I have:
<ul>
{% for section in sectList %}
{% autoescape off %}
<li class="middle_table_li">
<table class="middle_table">
<tr>
<td colspan="2" class="middle_table_cell">
<a href="" class="middle_a_dark">
{{ section.Identifier }}
</a>
</td>
</tr>
<tr>
{% for view in section.Identifier %}
<div style="padding: 0px; margin: 0px; width: 100%;">
{{ view.title }}
<table style="text-align: left;">
<tr>
<td>
<a href="{{ view.url }}" class="left_a_light">{{ view.title }}</a>
</td>
</tr>
</table>
</div>
{% endfor %}
</tr>
</table>
</li>
{% endautoescape %}
{% endfor %}
</ul>
When I use "{% for view in section.Identifier %}", view.title returns the word "homeList", split one character per iteration (h, o, m, e, etc.). When I try "{% for view in {{section.Identifier}} %}" it gives me the below error:
TemplateSyntaxError at /
Could not parse the remainder: '{{section.Identifier}}' from '{{section.Identifier}}'
My question is how can I use the results form one queryset within the constructor of a for loop? In the template, section.Identifier = 'homeList', and the view successfully sends the homeList data over, but instead of using the homeList variable from the view, it appears to split the string into a list, like list("homeList"), and the result is that each row consists of a single character from the word homeList. In regular python code I could use something like exec to execute the string as code, but I am unsure of how to do this in a django template, if it is even possible at all?
EDIT #1 (FYI, all data is passed successfully to the template)
views.py
# -*- coding: utf-8 -*-
from django.shortcuts import render
from django.db import connection, transaction
from app_HomeView.models import HomeList
from view_LeftPanel.models import LeftPanelList
def HomeView(request):
homeList = HomeList.objects.all().order_by("title")
cursor = connection.cursor()
cursor.execute("SELECT * FROM view_leftpanelview_sectionlist ORDER BY Id;")
desc = cursor.description
sectList = [dict(zip([col[0] for col in desc], row)) for row in cursor.fetchall()]
return render(request, "index.html", {"homeList": homeList,
"sectList": sectList,})