How to make add replies to comments in Django?

前端 未结 2 1719
有刺的猬
有刺的猬 2021-02-10 14:54

I\'m making my own blog with Django and I already made a Comments system.. I want to add the replies for each comment (like a normal comment\'s box) and I don\'t know what to do

2条回答
  •  慢半拍i
    慢半拍i (楼主)
    2021-02-10 15:44

    I had the same problem and resolved it as follows:

    1. For admin site as mentioned above just set blank=True for parent field. My comment model:

    class Comment(models.Model):
        post = models.ForeignKey(Post, related_name='comments')
        name = models.CharField(max_length=80)
        email = models.EmailField(max_length=200, blank=True)
        body = models.TextField()
        created = models.DateTimeField(auto_now_add=True)
        updated = models.DateTimeField(auto_now=True)
        # manually deactivate inappropriate comments from admin site
        active = models.BooleanField(default=True)
        parent = models.ForeignKey('self', null=True, blank=True, related_name='replies')
    
        class Meta:
            # sort comments in chronological order by default
            ordering = ('created',)
    
        def __str__(self):
            return 'Comment by {}'.format(self.name)
    
    • remember to run makemigrations and migrate

    2.Let's start with views. I'm using the post_detail view to display the post and its comments. We add a QuerySet to retrieve all parent active comments for this post. After this, we validate the submitted data using the form's is_valid(). If the form is valid we check if submitted data comes from hidden input in replay button form. Next if parent_id exits we create parent object(parent_obj) for replay comment and replay_comment object, then we assign parent_obj to replay_comment. If parent_obj is equal to None we just proceed with normal comment by creating new_comment object and saving it to the database.

    def post_detail(request, post):
        # get post object
        post = get_object_or_404(Post, slug=post)
        # list of active parent comments
        comments = post.comments.filter(active=True, parent__isnull=True)
        if request.method == 'POST':
            # comment has been added
            comment_form = CommentForm(data=request.POST)
            if comment_form.is_valid():
                parent_obj = None
                # get parent comment id from hidden input
                try:
                    # id integer e.g. 15
                    parent_id = int(request.POST.get('parent_id'))
                except:
                    parent_id = None
                # if parent_id has been submitted get parent_obj id
                if parent_id:
                    parent_obj = Comment.objects.get(id=parent_id)
                    # if parent object exist
                    if parent_obj:
                        # create replay comment object
                        replay_comment = comment_form.save(commit=False)
                        # assign parent_obj to replay comment
                        replay_comment.parent = parent_obj
                # normal comment
                # create comment object but do not save to database
                new_comment = comment_form.save(commit=False)
                # assign ship to the comment
                new_comment.post = post
                # save
                new_comment.save()
                return HttpResponseRedirect(post.get_absolute_url())
        else:
            comment_form = CommentForm()
        return render(request,
                      'core/detail.html',
                      {'post': post,
                       'comments': comments,
                       'comment_form': comment_form})
    

    Simple comment form:

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

    * More about ModelForm

    And lastly template. We need to create two forms. One form for comments and the second one for replays. Here you have simple template:

     
    

    Add a new comment

    {{ comment_form.as_p }} {% csrf_token %}
    {% for comment in comments %}

    {{ comment.name }} | {{ comment.created }}

    {{ comment.body|linebreaks }} {% for replay in comment.replies.all %}

    {{ replay.name }} | {{ replay.created }}

  • {{ replay.body }}
  • {% endfor %}
    Replay
    {{ comment_form.as_p }} {% csrf_token %}
    {% empty %}

    There are no comments yet.

    {% endfor %}

    just add some nice css and maybe jquery to have fade in reply comments and that's all.

提交回复
热议问题