简体   繁体   English

Django 循环模板标签不起作用

[英]Django loop template tag does not work

I've just started playing with Django and I can't seem to get the template tags to work at all.我刚刚开始玩 Django,我似乎根本无法使用模板标签。

Here's my code:这是我的代码:

views.py视图.py

def bookpage(request):
    query_results = Books.objects.all()
    data = query_results.values()
    data['title']=query_results.values('title')
    data['authors']=query_results.values('authors')
    return render (request,'index.html',data)

bookpage.html书页.html

<div class="resp-tabs-container">
           <tr>
             <th>Header 1</th>
             <th>Header 2</th>
           </tr>
           {% for x in query_results %}
           <tr>
             <td>{{title.x}}</td>
             <td>{{authors.x}}</td>
           </tr>
           {% endfor %}

When I run the code, the whole row ie, the one with the tags did not show at all.当我运行代码时,整行,即带有标签的那一行根本没有显示。

Please enlighten me!请赐教!

x is the object and title and authors are the attributes. x是对象, titleauthors是属性。 You have put them backwards.你把它们倒退了。

{% for x in query_results %}
    <tr>
        <td>{{x.title}}</td>
        <td>{{x.authors}}</td>
    </tr>
{% endfor %}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM