Irmantas Želionis
Irmantas Želionis

Reputation: 2324

Comments are not showing up

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

Answers (1)

patsweet
patsweet

Reputation: 1558

Do one of the following:

  1. Change your template to

    {% for a in comment %}

  2. Change your view to return comments instead of comment

    return render(request, 'task-result.html', { 'form': form, 'comments': comments, })

Upvotes: 2

Related Questions