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

查看:181
本文介绍了在数据库中显示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?

感谢

推荐答案

最简单的方法是使用 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部分

This is all covered in Part 3 of the Django tutorial. And here's Part 1 if you need to start there.

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

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