你如何使用views.py在django中显示mysql数据?

时间:2012-10-30 16:59:34

标签: python mysql django

我是python的初学者,我正在尝试下面的教程: http://net.tutsplus.com/tutorials/python-tutorials/python-from-scratch-creating-a-dynamic-website/

我创建了一个包含以下主体的模板:

<div class="container">
    <h1>First Blog</h1>
    <hr />
    {% for post in posts %}
        <div class="post">
        <h2>{{ post.title }}</h2>
        <h3>Posted on {{ post.timestamp }} by {{ post.author }}</h3>
        <p>{{ post.bodytext }}</p>
        </div>
        <hr />
    {% endfor %}
</div>

views.py文档包含以下代码:

from django.shortcuts import render_to_response
from blog.models import posts
def home(request):
    entries = posts.objects.all()[:10]
    return render_to_response('index.html', {'posts' : entries})

该网站显示以下内容:

First Blog

{% for post in posts %}
{{ post.title }}

Posted on {{ post.timestamp }} by {{ post.author }}

{{ post.bodytext }}

{% endfor %}

如何让网站显示数据库中的数据?

0 个答案:

没有答案