使用Django显示表中的数据 [英] Display data from a table using Django

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

问题描述

有人可以给我一小段代码,在其中显示表中的数据,并指向指向 views.py templates / index.html 显示目录?

Can someone please give me a small piece of code where we display data from a table and point me to code that goes into views.py and templates/index.html to display the table of contents?

推荐答案

阅读Django教程。它分4部分显示了该框架的基础。

Read the Django tutorial. It shows, in 4 parts, the basics of this framework.

对于您的问题,这是一个非常简单的示例。

As for your question, a very quick example.

在views.py:

def display(request):
  return render_to_response('template.tmpl', {'obj': models.Book.objects.all()})

在models.py中:

In models.py:

class Book(models.Model):
  author = models.CharField(max_length = 20)
  title = models.CharField(max_length = 40)
  publication_year = models.IntegerField()

在template.tmpl中:

In template.tmpl:

<table>
<tr>
  <th>author</th>
  <th>title</th>
  <th>publication year</th>
</tr>
{% for b in obj %}
<tr>
  <td>{{ b.author }}</td>
  <td>{{ b.title }}</td>
  <td>{{ b.publication_year }}</td>
</tr>
{% endfor %}
</table>

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

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