selected_book_views.py 文件源码

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

项目:Plamber 作者: OlegKlimenko 项目源码 文件源码
def add_comment(request):
    if request.is_ajax():
        comment_form = AddCommentForm(request.POST)

        if comment_form.is_valid():
            comment = BookComment.objects.create(id_user=TheUser.objects.get(id_user=request.user),
                                                 id_book=Book.objects.get(id=comment_form.cleaned_data['book']),
                                                 text=comment_form.cleaned_data['comment'])

            user = TheUser.objects.get(id_user=request.user)
            user_photo = user.user_photo.url if user.user_photo else ''

            logger.info("User '{}' left comment with id: '{}' on book with id: '{}'."
                        .format(user, comment.id, comment.id_book.id))

            response_data = {
                'username': escape(request.user.username),
                'user_photo': user_photo,
                'posted_date': comment.posted_date.strftime('%d-%m-%Y'),
                'text': escape(comment.text)
            }
            return HttpResponse(json.dumps(response_data), content_type='application/json')
    else:
        return HttpResponse(status=404)


# ----------------------------------------------------------------------------------------------------------------------
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号