Django,post 下方的评论表单有问题

Django, problem with the comment form below the post

我很长一段时间都想不通,我从管理面板对 posts 的评论做出了结论。但我只是不知道如何在 post 下方制作评论表单供用户发表评论。感谢大家!

models.py

class Post(models.Model):
    photo       = models.ImageField(upload_to='media/photos/',null=True, blank=True)
    name_barber = models.CharField(max_length=30)
    description = models.TextField(blank=True, null=True)

    def __str__(self):
        return self.description[:10]


class Comment(models.Model):
    post = models.ForeignKey(Post, related_name='comments', on_delete=models.CASCADE)
    name = models.CharField(max_length=30)
    body = models.TextField(null=True)
    add_date = models.DateTimeField(auto_now_add=True)
    enter code here

    def __str__(self):
        return '%s - %s' % (self.post, self.name)

form.py

class CommentForm(ModelForm):
    class Meta:
        model = Comment
        fields = ('name', 'body')

views.py

class HomePage(ListView):
    model = Post
    template_name = 'main/index.html' 
    context_object_name = 'posts1'

class BarbersPage(ListView):
    model = Post
    template_name = 'main/barbers.html' 
    context_object_name = 'posts'

urls.py

urlpatterns = [
    path('',views.HomePage.as_view(),name='index'),
    path('barbers/',views.BarbersPage.as_view(), name='barbers'),
  
]

barbers.html

{% for post in posts %}
          <img src="{{MEDIA_URL}}{{post.photo.url}}" width="800"  />

          <h3>
        {{ post.name_barber}} 
          </h3>
     <p>{{ post.description}}</p>



     <h3> Comments.. </h3>

    {% if not post.comments.all %}
    no comments yet...<a href = "#">Add one</a>

    {% else %}

        {% for comment in post.comments.all %}

    <strong>
        {{ comment.name }}
        {{ comment.add_date }}
    </strong>
        <p>{{comment.body }}</p>
<br>
        {% endfor %}
         
    {% endif %}           
        
{% endfor %}

您需要在 post 下面添加一个表单块,供用户添加评论。

像这样:

{% for post in posts %}
     <img src="{{MEDIA_URL}}{{post.photo.url}}" width="800"  />
          <h3>
        {{ post.name_barber}} 
          </h3>
     <p>{{ post.description}}</p>

     <h3> Comments.. </h3>

    {% if not post.comments.all %}
        no comments yet...<a href = "#">Add one</a>
    {% else %}
        {% for comment in post.comments.all %}
            <strong>
                {{ comment.name }}
                {{ comment.add_date }}
            </strong>
            <p>{{comment.body }}</p>
            <br>
        {% endfor %}         
    {% endif %}
    Add Comment:<br/>
    <form method="post" action="/AddComment">
        <input type="hidden" id="postid" value="{{post.id}}"/>
        <input type="text" id="newcomment" size="50"/><input type="submit" value="Submit"/>
    </form><br/>
{% endfor %}

您需要决定在哪里提交评论数据并更新表单中的操作属性。