今天我试着做一个评论部分,用户可以在这里发表评论。这是我的密码:
#views.py
def add_comment(request):
if request.method == 'POST':
form = CommentForm(request.POST)
if form.is_valid():
save_it = form.save()
save_it.save()
comments = Comment.objects.all()
return render(request, 'task-result.html', {
'form': form, 'comment': comments,
})
else:
form = CommentForm()
return render(request, 'Task-form.html', {
'form': form,
})
#HTML
<body>
<h3>Comments</h3>
{% for a in comments %}
<li>{{ a.body }}</li>
{% endfor %}
{% csrf_token %}
</body>然而,什么都没有打印出来。怎么啦?
发布于 2014-03-21 20:40:28
执行下列操作之一:
comments而不是comment
返回呈现(请求,‘任务-结果. form’,{‘表单’:表单,‘注释’:注释,})https://stackoverflow.com/questions/22569064
复制相似问题