【问题标题】:Dictionary to table in DjangoDjango中的字典到表格
【发布时间】:2014-05-22 18:12:08
【问题描述】:

我是 Django 的新手,到目前为止学习它很困难。即使有很多文档,我有时也无法将它们放在一起。现在我被困在把字典放在桌子上......(是的,我已经完成了教程)

Django 1.6 和 python 2.7

查看:

def panel(request):
 adlist = {}
 adlist['title'] = 'haas','paas'
 adlist['price'] = 12,50
 adlist['bid'] = 50,0
 adlist['seen'] = 23,11
 context = {'adlist' : adlist}
 return render(request, 'panel.html', context)enter code here

模板(我尝试了很多变体):

<table class="zebra">
<caption>Panel.</caption>
    <thead>
        <tr>
            <th>title</th>
            <th>price</th>
            <th>bid</th>
            <th>seen</th>
        </tr>
    </thead>
    <tbody>
    {% for ad in adlist %}
        <tr>

            <td>{{ ad.title }}</td>
            <td>{{ ad.price}}</td>
            <td>{{ ad.bid}}</td>
            <td>{{ ad.seen}}</td>

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

【问题讨论】:

标签: python django dictionary html-table


【解决方案1】:

你需要制作一个广告列表而不是字典,例如:

def panel(request):
    adlist = [{'title': 'haas', 'price': 12.50, 'bid': 50.0, 'seen': 23.11}]
    return render(request, 'panel.html', {'adlist' : adlist})

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-05-29
    • 2016-04-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-04-19
    相关资源
    最近更新 更多