作为数据库查询的变量无法在模板中正确呈现

时间:2013-05-11 10:40:07

标签: django django-models django-templates

我的模板不会呈现变量表的键值,这是一个字典。

{% for key, value in table.items %}
<p> {{key}} : {{value}}</p>
{% endfor %}

变量'table'是如何派生的。客户端是模型,Client_FirstName是模型的属性。

table = Client.objects.filter(Client_FirstName__startswith='p').values()

我正在尝试进行数据库查询,我只是从here

中学习

2 个答案:

答案 0 :(得分:1)

数据库中可能没有任何数据。试试这个。

{% if table|length %}
    {% for key, value in table.items %}
        <p> {{ key }} : {{ value }}</p>
    {% endfor %}
{% else %}
    <div>There are no data in the database</div>
{% endif %} 

答案 1 :(得分:0)

我在模板上错误地渲染了数据库值,这就是字典列表

应该怎么做
    {% for x in table %}
     {% for key,values in x.items %}
     <p> {{key}} : {{values}} </p>
     {% endfor %}
   {% endfor %}