本文介绍了使用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?
推荐答案
。它分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显示表中的数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!