Reputation: 2324
Today I am trying to make comments section, where users can post comments. This is my code:
#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>
However, nothing is printed out. What is wrong?
Upvotes: 0
Views: 46
Reputation: 1558
Do one of the following:
Change your template to
{% for a in comment %}
Change your view to return comments
instead of comment
return render(request, 'task-result.html', { 'form': form, 'comments': comments, })
Upvotes: 2