Home >Web Front-end >JS Tutorial >Implementation code for django ajax submit comments and automatically refresh function
This article mainly introduces the implementation code of django ajax submission comment and automatic refresh function. Friends who need it can refer to it
After trying many times, I finally got it, let’s upload the code. (I use jQuery's ajax, not native)
js code:
<script> $(document).ready(function () { getcomment(); $('.comment-box button').click(function () { var comment_text = $('.comment-box textarea').val(); $.ajax({ type: 'POST', url: '/bbs/article/{{ article_list.id }}/comment/', data: {comment: comment_text}, success:function (callback) { var data = $.parseJSON(callback); $('.callback').html(data.result); if(data.result === 'successfully') { getcomment(); } } }) }); }); function getcomment() { $.ajax({ type: 'GET', url: '/bbs/article/{{ article_list.id }}/get_comment/', success:function (call) { var datas = $.parseJSON(call); $('.comment-list').html(datas.answer); } }) } </script>
Call the getcomment() function after the full text is loaded, get the comments from the database, and submit the comments written by yourself Then call the getcomment() function again to automatically refresh the
html template (the bootstrap template is used):
<p class="row"> <p class="comment-list" style="margin-left: 10px"> </p> </p> <p class="row"> <article class="col-xs-12"> <h4>请评论:</h4> <p class="comment-box"> <textarea class="form-control" rows="3"></textarea> <span class="callback"></span><button type="submit" class="btn btn-success pull-right" style="max-width: 5px;">评论</button> </p> </article> </p> <hr>
View function:
@csrf_exempt def comment(request,article_id): if request.method == 'POST': comments = request.POST['comment'] if len(comments) < 5: result = u'评论数需大于5' return HttpResponse(json.dumps({'result': result})) else: result = 'successfully' Comment.objects.create(content= comments, article_id=article_id) return HttpResponse(json.dumps({'result': result}))
This is the function to submit comments, Don’t forget to add the csrf decorator
def get_comment(request, article_id): article_list = get_object_or_404(Article, id=article_id) comments = article_list.comment_set.all() html = '' for i in comments: ele = '<p class="row"><article class="col-xs-12"><p class="pull-right"><span class="label label-default">作者:' + 'i.user' + '</span></p><p>' + i.content + '<ul class="list-inline"><li><a href="#" rel="external nofollow" ></a></li></ul></article></p><hr>' html += ele return HttpResponse(json.dumps({'answer': html}))
Function to get comments in the background.
Finally clear the value of textarea:
function resettext() { $('.form-control').val(''); }
The above is what I compiled for everyone. I hope it will be helpful to everyone in the future.
Related articles:
The reason why adding a random number after the ajax request address prevents browser caching
JS clears the IE browser cache Method
The above is the detailed content of Implementation code for django ajax submit comments and automatically refresh function. For more information, please follow other related articles on the PHP Chinese website!