0

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']
M. Phys
  • 139
  • 15
  • The class `PostDetailView()` should implement a `post` and `get` function: https://stackoverflow.com/questions/52244156/method-not-allowed-post-in-django – Sy Ker Jan 30 '22 at 18:41
  • Look like you are trying to achieve something similar to this :- https://stackoverflow.com/questions/45659986/django-implementing-a-form-within-a-generic-detailview checkout the above ans. – Shreeyansh Jain Jan 30 '22 at 18:44

1 Answers1

2

Don't mix class-based-view with method-based-view. You have to provide function that reads method and behave as it should. For get you usually will have built-in solutions, like form_class - in post() you can call it via form = self.get_form(). It generally looks like this:

class PostDetailView(DetailView):
    model = Post
    form_class = CommentForm

    def post(self, request, *args, **kwargs):
        form = self.get_form()
        print('Get the data from the POST request')
        print(form)

    def get_context_data(self, **kwargs):
        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

Another things to check ie. here: https://stackoverflow.com/a/45661979/12775662

NixonSparrow
  • 6,130
  • 1
  • 6
  • 18