selected_book_views.py 文件源码

python
阅读 25 收藏 0 点赞 0 评论 0

项目:Plamber 作者: OlegKlimenko 项目源码 文件源码
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)
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号