无法在Django中使用ModelForm在帖子中添加评论

views.py

def postdetail(request,pk): # Single Post view.

    post = Post.objects.get(id=pk)
    comment = post.comments.all()
    comment_count = comment.count()

    if request.user.is_authenticated:

        if request.method == 'POST':
            form = CommentForm(data=request.POST)
            content = request.POST['cMessage']

            if form.is_valid():
                print("Yes valid")
                form.instance.body = content
                new_comment = form.save(commit=False)
                print(new_comment)
                new_comment.post = post
                new_comment.user = request.user
                new_comment.save()
                return redirect('blog-home')
        else:

            form = CommentForm()

    context = {
        'comment_form': CommentForm,
        'post' : post,
        'comments': comment,
        'count': comment_count,
    }

    return render(request,'post/postdetail.html', context=context)

models.py

class Comment(models.Model):

    post = models.ForeignKey(Post, on_delete=models.CASCADE, related_name='comments')
    user = models.ForeignKey(User,on_delete=models.CASCADE, related_name='comments')
    body = models.TextField()
    created = models.DateTimeField(auto_now_add=True)
    updated = models.DateTimeField(auto_now=True)
    # active = models.BooleanField(default=True)

    class Meta:
        ordering = ('created',)

    def __str__(self):
        return f'Comment by {self.user} on {self.post}'

表格

class CommentForm(forms.ModelForm):
    class Meta:
        model = Comment
        fields = ['body']

模板

{% if request.user.is_authenticated %}
    <!-- respond -->
    <div class="respond">
        <h3>Leave a Comment</h3>
            <!-- form -->
                <form name="contactForm" id="contactForm" method="post" action="">
                {% csrf_token %}
                <fieldset>

                    <div class="message group">
                        <label  for="cMessage">Message <span class="required">*</span></label>
                        <textarea name="cMessage"  id="cMessage" rows="10" cols="50" ></textarea>
                    </div>

                    <button type="submit" class="submit">Submit</button>

                </fieldset>
                </form> <!-- Form End -->
    </div> 
{% endif %}

如果我使用外壳程序/通过管理面板添加评论,但是如果我尝试通过表单动态添加评论,则不会保存该评论,也不会显示任何错误。 我仅在模板中添加了表单。