如何在vi中用游标显示查询集结果

2024-04-20 14:28:28 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个函数,它运行一个查询并传递要在视图中显示的结果

 def index(request):
        data = dict()    
        cursor = connection.cursor()
        cursor.execute('''SELECT user.email, item.id, count(item.author_id)
                          FROM item INNER JOIN user on item.user_id = user.id
                          GROUP BY item.author_id ''')
        data['item'] = cursor.fetchall();

        return render(request, 'ideax/panel.html', data)

查询集结果示例:

[('teste@gmail.com', 1, 4), ('admin@gmail.com', 2, 5)]

如何在我的panel.html中显示此查询的结果

我试过这个,但没用:

{% for d in item %}

    {{d.email}}

{% endfor %}

Tags: 函数com视图iddataemailrequestdef
1条回答
网友
1楼 · 发布于 2024-04-20 14:28:28

它正在返回元组,所以我们必须像下面那样解包

{% for email, item_id, count in item %}
    {{email}}
{% endfor %}

或者我们也可以使用索引

{% for d in item %}
    {{ d.0 }}
{% endfor %}

相关问题 更多 >