0

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,})
4

2 回答 2

1

我会说这个系列应该看起来更像

sectList = {"Id": 1, "Section": "Home",
"Identifier": {"Id": 1, "Title": "Home", 
"Url": "home/","Id": 2, "Title": "My Account",
 "Url": "account/","Id": 3, "Title": "Full List",
 "Url": "fullList/",}}

你看 - "Identifier": "homeList" 并不是你的第二个列表的真正链接。它只是一个道具。名称和值。

于 2013-07-29T19:21:53.013 回答
1

的,您可以,但是您的视图代码传递了错误的值,这就是它不起作用的原因。你有:

sectList = [{"Id": 1, "Section": "Home", "Identifier": "homeList"}]

然后在模板中,在第一个循环之后,结果section.Identifier显然是"homeList",所以就像你运行:

for i in "homeList":
    print i

它会打印出“homeList”字符串的每个字符。

所以你需要做的是将列表嵌套在homeList里面secList,你secList应该是这样的:

sectList = [{ 
    "Id": 1, "Section": "Home", "Identifier": [  # Identifier should be a list
        {"Id": 1, "Title": "Home", "Url": "home/"},
        {"Id": 2, "Title": "About", "Url": "about/"}
    ] 
}]

这是你的挑战,对吧?

于 2013-07-29T23:28:16.870 回答