def load_comments(request):
if request.is_ajax():
form = LoadCommentsForm(request.POST)
if form.is_valid():
book = Book.objects.get(id=form.cleaned_data['book_id'])
comments = BookComment.objects.filter(id_book=book).order_by('-id')
next_page_num = form.cleaned_data['page'] + 1
comments_paginator = Paginator(comments, COMMENTS_PER_PAGE)
page = comments_paginator.page(next_page_num)
json_comments = [{
'username': escape(comment.id_user.id_user.username),
'user_photo': comment.id_user.user_photo.url if comment.id_user.user_photo else '',
'posted_date': comment.posted_date.strftime('%d-%m-%Y'),
'text': escape(comment.text)
} for comment in page.object_list]
response_data = {
'comments': json_comments,
'current_page': next_page_num,
'has_next_page': page.has_next(),
'book_id': book.id
}
return HttpResponse(json.dumps(response_data), content_type='application/json')
else:
return HttpResponse(status=404)
评论列表
文章目录