Как добавить раздел комментариев в Django с помощью формы модели

Я новичок в Django Я начал проект блога Я хотел добавить функцию раздела комментариев в моем проекте У меня ошибка Режим формы не определен в классе

view.py

from Blog.models import Comment 
from Blog.forms import CommentForm 
def post_detail_view(request,year,month,day,post): 
    post=get_object_or_404(Post,slug=post, 
    status='published', 
    publish__year=year, 
    publish__month=month, 
    publish__day=day) 
    comments=Comment.objects.filter(active=True) 
    csubmit=False 
    if request.method=='POST': 
        form=CommentForm(data=request.POST) 
        if form.is_valid(): 
            new_comment=form.save(commit=False) 
            new_comment.post=post 
            new_comment.save() 
            csubmit=True 
    else:
        form=CommentForm() 
    return render(request,'blog/detail.html',{'post':post,'comments':comments,'csubmit':csubmit,'form':form})

если я попытаюсь запустить эту функцию, я хочу отобразить форму на детальной странице и если конечный пользователь отправит форму, то отобразить ту же страницу
detail.html

<!doctype html> {% extends "blog/base.html" %} {% block title_block %} detail Page {% endblock%} {% block body_block %}
<h1>This Is Your Content</h1>
<div>
    <h2>{{post.title|title}}</h2>
</div>
<div>
    {{post.body|title|linebreaks}}
    <p>Publised of {{post.publish|title}} Published By {{post.author|title}}</p>
    <div>
        {% with comments.count as comments_count %}
        <h2>{{comments_count}} Comment{{comments_count|pluralize}}</h2>
        {% endwith%}
        <div>
            {%if comments %} {%for comment in comments %}
            <p> comment {{forloop.counter}} by {{comment.name}} on {{comment.created}}
            </p>
            <div class="cb">{{comment.body|linebreaks}}</div>
            <hr> {%endfor%} {%else%}
            <p>There are NO Comments Yet !!!</p>
            {%endif%} {%if csubmit %}
            <h2>Your Comment Added Succefully</h2>
            {%else%}
            <form method="post">
                {{form.as_p}} {%csrf_token%}
                <input type="submit" name="" value="Submit Comment">
            </form>
            {%endif%}

        </div>
        {%endblock%}

здесь я определил свою форму forms.py

from Blog.models import Comment
class CommentForm(ModelForm):
    class meta:
        model=Comment
        fields=('name','email','body')

связанная модель, которую я упоминаю ниже models.py

class Comment(models.Model):
    post=models.ForeignKey(Post, related_name=('comments'), on_delete=models.CASCADE)
    name=models.CharField( max_length=32)
    email=models.EmailField()
    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 'Cammnted by{} on {}'.format(self.name,self.post)

Обновите ваш файл forms.py на этот, и он будет работать

from django.db.models import fields
from django import forms
from Blog.models import Comment

class CommentForm(forms.ModelForm):
    class meta:
        model=Comment
        fields=('name','email','body')
Вернуться на верх