【问题标题】:How to access a list and dictionary in same for loop at template level in Django如何在Django的模板级别访问同一for循环中的列表和字典
【发布时间】:2012-04-09 22:03:32
【问题描述】:

我在view.py 中有一个函数,它返回两个变量,即一个名为ca 的字典和一个名为categories 的列表:

def eventcateg_detail(request):
    ca = EventTypeCategory.objects.values()
    categories =[]
    for i in range(0, len(ca)):
       pk_sub = ca[i]['sub_categ_id']
       if (pk_sub!=None):
          category = EventTypeCategory.objects.get(id = pk_sub)
          category = category.name
          categories.append(category)
    return render(request,"events/categ.html",{
      'obj': ca,
      'categ' :categories
      })

categ 返回:

[u'Critical in list', u'Information in list', u'Amit Pal in list']

我的categ.html 是:

<thead>
        <tr><th>{% trans "Name Category" %}</th><th>{% trans " Message" %}</th><th>{% trans "Sub-categories" %}</th></tr>
    </thead>
    <tbody>
    {% for obj in obj %}
        <tr>

            <td>{{ obj.name }}</td><td> {{ obj.Message_slug }}</td><td></td>

        </tr>
    {% endfor %}
    </tbody>
</table>

我的输出应该是:

Event Category  Event Message   Sub-categories
Critical            critical    critical in list
Information         information information in list
Amit Pal            amit-pal    amit pal in list

子类别包含来自列表的数据。我得到Event Category(使用obj.name)和Event Message(使用obj.Message_slug)是对的,但我无法找到任何方法将子类别也放在同一个td和相同的for循环中(看模板)。

【问题讨论】:

  • 匿名投票者,为什么讨厌?这不是一个坏问题。

标签: python django django-templates django-views


【解决方案1】:

我建议你看看这个:Django, category and subcategories

这可能需要对您的模型进行一些更改,但会大大简化您的代码。

更新

那么我会回答你的第一个问题:)

在你的情况下,你可以嵌套 for 循环:

<thead>
    <tr><th>{% trans "Name Category" %}</th><th>{% trans " Message" %}</th><th>{% trans "Sub-categories" %}</th></tr>
</thead>
<tbody>
{% for obj in obj %}
    <tr>

        <td>{{ obj.name }}</td><td> {{ obj.Message_slug }}</td>
        <td>
            {% for i in categ %}
                  {{ i }}
            {% endfor %}
        </td>

    </tr>
{% endfor %}
</tbody>

但是!上面的代码指出了 eventcateg_detail 方法的一个弱点。您创建的列表不会告诉列表项目属于哪个父类别,因此对于每个父对象,所有项目都将被打印。

如果你知道永远存在一对一的关系并且永远只有一个孩子,那么你可以创建一个字典来代替

另外,我相信您的初始代码可以简化:

def eventcateg_detail(request):
        ca = EventTypeCategory.objects.all()
        categories ={}
        for cat in ca:
            if cat.sub_categ_id:

               #Since the ca queryset already contains all items you can use that to get the child category
               category = ca.objects.get(id=cat.sub_categ_id)

               #Add to dictionary. NOTE: If you ever get more than one sub-category 
               #you need to build a list of sub-categories before adding it to the dict.
               categories[cat.id] = category.name
        return render(request,"events/categ.html",{ 'obj': ca, 'categ' :categories})

在你的模板中你这样做:

<thead>
<tr>
     <th>{% trans "Name Category" %}</th>
     <th>{% trans " Message" %}</th><th>{% trans "Sub-categories" %}</th>
</tr>
</thead>
<tbody>
{% for o in obj %}
<tr>
    <td>{{ o.name }}</td><td> {{ o.Message_slug }}</td>
    <td>
        {% for key, value in categ.items %}
              {% ifequal key o.id %}
               {{ value }}
              {% endifequal %}
        {% endfor %}
    </td>

</tr>

{% endfor %}

【讨论】:

  • 感谢您的回复。但我的问题与那个不同。我无法找出已经在 for 循环之间放置列表
  • 您能否更具体地说明该答案如何不适用于您的问题?马上,这对我来说似乎是一个很好的答案,所以也许我们缺少关于您的要求的一些东西。
  • 我试图猜测你想要什么。请参阅上面的更新。
猜你喜欢
  • 2018-09-22
  • 2019-07-16
  • 2015-08-18
  • 2021-10-17
  • 1970-01-01
  • 1970-01-01
  • 2011-09-19
  • 2018-09-11
  • 2016-08-31
相关资源
最近更新 更多