在数据库中显示Django中的表 [英] Displaying a Table in Django from Database

查看:173
本文介绍了在数据库中显示Django中的表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在网页上以表格格式从数据库表中显示信息?有没有一个简单的方法来做这个在django或它需要一个更复杂的方法。更具体地说,你如何将一个数据库表中的列和行几乎移植到一个可以从url看到的视觉表?

How do you display the information from a database table in a table format on a webpage? Is there a simple way to do this in django or does it require a more complicated approach. More specifically, how do you pretty much port over the columns and rows in a database table to a visual table that can be seen from a url?

推荐答案

最简单的方法是使用循环模板标签

The easiest way is to use a for loop template tag.

给定视图:

def MyView(request):
    ...
    query_results = YourModel.objects.all()
    ...
    #return a response to your template and add query_results to the context

您可以添加这样的代码片段...

You can add a snippet like this your template...

<table>
    <tr>
        <th>Field 1</th>
        ...
        <th>Field N</th>
    </tr>
    {% for item in query_results %}
    <tr> 
        <td>{{ item.field1 }}</td>
        ...
        <td>{{ item.fieldN }}</td>
    </tr>
    {% endfor %}
</table>

这一切都在第3部分的Django教程。如果您需要从那里开始,请第1部分

这篇关于在数据库中显示Django中的表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆