如何在Django模板中遍历两元字典

时间:2015-11-10 03:43:00

标签: django templates

1.我的观点有一个词典,例如:

category_all = {
    u'Python': {
        'acticle_info': [
            [<Article: python_tump>],[<Article:python_dict>]
         ],
         'article_count': 22
     }
}

2.my render is:

return render(request, 'blog/index.html',{'category_all':category_all})

3.i遍历Django模板中的dict:

  <!-- tabs Nav  -->
  <ul id="myTab" class="nav nav-tabs" role="tablist">
    {% for blog_category in category_all.keys %}
       <li role="presentation"><a href="#{{ blog_category }}" aria-controls="{{ blog_category }}" role="tab" data-toggle="tab">{{ blog_category }}</a></li>
    {% endfor %}
  </ul>
  <!-- Tab panes -->
  <div id="myTabContent" class="tab-content">
    {% for blog_category,blog_info in category_all.items %}
    <div role="tabpanel" class="tab-pane" id="{{ blog_category }}">
        <table class="table table-hover">
            <tbody>
                {% for get_blog_info in blog_info.acticle_info %}
                    {% for blog_result  in get_blog_info %}
                        <tr class="active">
                            <td><a href="{% url 'blog_detail' num=blog_result.pk %}">{{ blog_result.title }}</a></td>
                            <td>{{ blog_result.date_publish }}</td>
                        </tr>
                    {% endfor get_blog_info %}
                {% endfor %}
            </tbody>
        </table>
    </div>
    % endfor %}
</div>

4.help 4.1。如何获得article_count值

4.2。我使用了很多for循环,如何减少for循环

1 个答案:

答案 0 :(得分:0)

您可以通过以下方式获得SELECT `userId` FROM `openEndedResponses` AS `oe` WHERE `oe`.`questionId` = 3 -- zip code AND (REPLACE( REPLACE( `oe`.`value`, ' ', '' ), '-', '' ) IN ( '30071', '30106', '30122', '30134', '30135', '30168', '30180', '30185', '30187', '30317', '30004' )); 值:

article_count

更改 blog_info.article_count 结构以减少循环!