How to make add replies to comments in Django?

前端 未结 2 1743
时光取名叫无心
时光取名叫无心 2021-02-10 15:05

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条回答
  •  梦如初夏
    2021-02-10 15:55

    first Question:parent must be set in admin.

    parent = models.ForeignKey('self', null=True, blank=True, related_name='replies')
    

    blank=True can let you don't set parent in admin.

    second Question:add comment dynamicly.

    {% csrf_token %}
    $('#comment-form').submit(function(){ $.ajax({ type:"POST", url:"{% url 'article_comments' article.en_title %}", data:{"comment":$("#comment").val()}, beforeSend:function(xhr){ xhr.setRequestHeader("X-CSRFToken", $.cookie('csrftoken')); }, success:function(data,textStatus){ $("#comment").val(""); $(".comment ul").prepend(data); }, error:function(XMLHttpRequest, textStatus, errorThrown){ alert(XMLHttpRequest.responseText); } }); return false; });

    view.py:

        print_comment = u"

    comment:{}

    ".format(text) if parent: print_comment = u"
    \

    \ @{}\ {}\

    \
    ".format( parent.user.username, parent.text ) + print_comment # current comment html = u"
  • \
    \ \
    \
    \

    {}

    \ {}\

    {}

    \
    \
  • ".format( img, comment.user.username, print_comment, datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S") ) return HttpResponse(html)

提交回复
热议问题