'Comment POST request working but comments not appearing under post
I'm relatively new to all this. I'm in the process of creating a social media page with django/python. The ability to create a post in a news feed (i.e. list of posts) and the ability to click on one post to see a more detailed view is working properly. When a user clicks on a single post to see it in more detail they also have the ability to add a comment.Here's the problem: the 'create a comment' input box is working fine, as is the 'post comment' button, but when you click the 'post comment' button my terminal does show that the comment is being posted, but the list of comments never appears.
What appears in my Powershell/Terminal when I click the 'post comment' button:
[DD/MONTH/2022 HH:MM:SS] "POST /social/post/5 HTTP/1.1" 200 3995
When the purple 'Post' button is clicked, the page refreshes, but the comment is never displayed below, nor does the comment box get cleared.

My settings.py installed apps:
INSTALLED_APPS = [
'social',
'landing',
'crispy_forms',
'allauth',
'allauth.account',
'allauth.socialaccount',
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'django.contrib.sites',
'crispy_bootstrap5',
'allauth.socialaccount.providers.instagram',
'allauth.socialaccount.providers.linkedin',
'allauth.socialaccount.providers.facebook',
]
My views.py:
from django.shortcuts import render
from django.urls import reverse_lazy
from django.contrib.auth.mixins import UserPassesTestMixin, LoginRequiredMixin
from django.views import View
from django.views.generic.edit import UpdateView, DeleteView
from .models import Post, Comment
from .forms import PostForm, CommentForm
class PostDetailView(LoginRequiredMixin, View):
def get(self, request, pk, *args, **kwargs):
post = Post.objects.get(pk=pk)
form = CommentForm()
comments = Comment.objects.filter(post=post).order_by('-created_on')
context = {
'post': post,
'form': form,
'comments': comments,
}
return render(request, 'social/post_detail.html', context)
def post(self, request, pk, *args, **kwargs):
post = Post.objects.get(pk=pk)
form = CommentForm(request.POST)
if form.is_valid():
new_comment = form.save(commit=False)
new_comment.author = request.user
new_comment_post = post
new_comment.save()
comments = Comment.objects.filter(post=post).order_by('-created_on')
context = {
'post': post,
'form': form,
'comments': comments,
}
return render(request, 'social/post_detail.html', context)
My models.py:
from django.db import models
from django.utils import timezone
from django.contrib.auth.models import User
class Post(models.Model):
body = models.TextField()
created_on = models.DateTimeField(default=timezone.now)
author = models.ForeignKey(User, on_delete=models.CASCADE)
class Comment(models.Model):
comment = models.TextField()
created_on = models.DateTimeField(default=timezone.now)
author = models.ForeignKey(User, on_delete=models.CASCADE)
post = models.ForeignKey('Post', on_delete=models.CASCADE, null=True)
My forms.py:
from django import forms
from .models import Post, Comment
class PostForm(forms.ModelForm):
body = forms.CharField(
label='',
widget=forms.Textarea(
attrs={'rows': '3',
'placeholder': 'Share your ideas...'}
))
class Meta:
model = Post
fields = ['body']
class CommentForm(forms.ModelForm):
comment = forms.CharField(
label='',
widget=forms.Textarea(
attrs={'rows': '3',
'placeholder': 'Share your opinion...'}
))
class Meta:
model = Comment
fields = ['comment']
The actual html template that the comment function is on:
post_detail.html:
{% extends 'landing/base.html' %}
{% load crispy_forms_tags %}
{% block content %}
<div class="row justify-content-center mt-3">
<div class="col-md-5 col-sm-12">
<h5>Add a Comment!</h5>
</div>
</div>
<div class="row justify-content-center mt-3 mb-5">
<div class="col-md-5 col-sm-12">
<form method="POST">
{% csrf_token %}
{{ form | crispy }}
<div class="d-grid gap-2">
<button class="btn btn-light" style="background-color: #CC64C3; mt-3 mb-5">Post</button>
</div>
</form>
</div>
</div>
{% for comment in comment %}
<div class="row justify-content-center mt-3 mb-5 border-bottom">
<div class="col-md-5 col-sm-12">
<p>
<strong>{{ comment.author }}</strong> {{ comment.created_on }}
{% if request.user == comment.author %}
<a href="{% url 'comment-delete' post.pk comment.pk %}" style="color: #333;"><i class="fas fa-trash"></i></a>
{% endif %}
</p>
<p>{{ comment.comment }}</p>
</div>
</div>
{% endfor %}
</div>
{% endblock content %}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
