+ -
当前位置:首页 → 问答吧 → 求助!!!

求助!!!

时间:2011-12-16

来源:互联网

我在django中的template中写了一个archive.html.
代码如下:
{ % for post in posts %}
<h2>{{ post.title }}</h2>
<p>{{ post.timestamp }}</p>
<p>{{ post.body }}</p>
{% endfor % }


View.py代码入下:
from django.template import loader,Context
from django.http import HttpResponse
from TestField.blog.models import BlogPost

def archive(request):
  posts = BlogPost.objects.all()
  t = loader.get_template("archive.html")
  c = Context({'posts':posts})
  return HttpResponse(t.render(c))


我打开浏览器后,为什么只显示出
{ % for post in posts %}和{% endfor % }
而中间的没有显示出来,这是为什么?????

作者: Mr_JJ_Lian   发布时间: 2011-12-16

确保posts是有值的

很简单自己调一下把

Python code

c = Context({'posts': posts})

# 改为

c = Context({'posts': [{'title': 'test', 'timestamp': '20111216', 'body': 'body'}]})



作者: userguanguan   发布时间: 2011-12-16