0

视图.py

from django.shortcuts import render_to_response
from models import Post
def getRecentPosts(request):
    posts = Post.objects.all()

    # sort by chronological order
    sorted_posts = posts.order_by('-pub_date')

    # display all posts
    return render_to_response('posts.html', {'posts': sorted_posts})

帖子.html

<html>
<head>
    <title>My Django Blog</title>
</head>
<body>
{% for post in posts %}
    <h1>{{post.title}}</h1>
    <h3>{{post.pub_date}}</h3>
    {{ post.text }}
{% endfor %}
</body>

此代码工作正常并打印正确的数据....但是,如果我改变

return render_to_response('posts.html', {'posts': sorted_posts})

return render_to_response('posts.html', {'po': sorted_posts})     

{% for post in posts %} to {% for post in po %}

在posts.html中

它无法生成任何数据.....那么视图中的字典名称与模板之间的关系是什么

4

0 回答 0