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)
# ----------------------------------------------------------------------------------------------------------------------
评论列表
文章目录