开发者

Display data from a table using Django [closed]

As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question wil开发者_如何学Pythonl likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance. Closed 10 years ago.

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?


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

As for your question, a very quick example.

In views.py:

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

In models.py:

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

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>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜