I'm trying to implement a feature where logged in users can comment on a blog post, but I'm a little stuck with the comments appearing on the page.
I can type in the comment and submit them. However, this is always returning a GET request. I tried adding method='post'
to both the form and button tags, but this resulted in the following:
Method Not Allowed (POST): /post/1/
Method Not Allowed: /post/1/
I'm not really sure what the problem is. Any help would be much appreciated. Code snippets below.
post_detail.html with comment section:
<div class="content-section">
<legend class="border-bottom">Comments:</legend>
{% if user.is_authenticated %}
<form id="commentForm">
{% csrf_token %}
<fieldset class="form-group">
<div class="row">
<!-- <div class="col"></div> -->
<div class="col-1 text-right">
<img class="img-comment" src="{{ user.profile.image.url }}">
</div>
<div class="col-9">
<textarea type="text" id="commentIn" name="comment"
placeholder="Your comment..." required></textarea>
</div>
<div class="col"></div>
</div>
<div class="row">
<div class="col-10 text-right">
<button id="commentBtn" name="comment_button"
class="btn btn-outline-info" type="submit">Comment</button>
</div>
<div class="col"></div>
</div>
<hr>
</fieldset>
</form>
{% endif %}
<div id="commentDiv">
{% for cmnt in comments_list %}
<img class="img-comment"
src="{{cmnt.commentator.profile.image.url }}">
<a class="mr-2" >{{ cmnt.commentator }}</a>
<p class="article-content">{{ cmnt.comment }}</p>
<hr>
{% endfor %}
</div>
</div>
urls.py in blog app
urlpatterns = [
path('post/<int:pk>/', PostDetailView.as_view(), name="post-detail"),
models.py
class Post(models.Model):
title = models.CharField(max_length=100)
content = models.TextField()
date_posted = models.DateTimeField(default=timezone.now)
author = models.ForeignKey(User, on_delete=models.CASCADE)
likes = models.ManyToManyField(User, related_name='like')
def __str__(self):
return self.title
def get_absolute_url(self):
return reverse('post-detail', kwargs={'pk': self.pk})
class Comment(models.Model):
commentator = models.ForeignKey(User, on_delete=models.CASCADE)
blog = models.ForeignKey(Post, on_delete=models.CASCADE)
comment = models.CharField(max_length=200)
date_added = models.DateTimeField(default=timezone.now)
class meta:
verbose_name_plural = 'comments'
def __str__(self):
return self.comment
views.py
class PostDetailView(DetailView):
model = Post
form = CommentForm()
def get_context_data(self, **kwargs):
if self.request.method == 'GET':
print('Render a blank form for users to fill out')
print(self.form)
elif self.request.method == 'POST':
print('Get the data from the POST request')
print(self.form)
status = 0
if self.request.user in self.object.likes.all():
status = 1
context = super().get_context_data(**kwargs)
context['comments_list'] = Comment.objects.filter(blog=self.object)
context['status'] = status
return context
and finally, the form itself:
class CommentForm(forms.ModelForm):
class Meta:
model = Comment
fields = ['comment']