Exemplo n.º 1
0
def add(request):
    '''
    обработка добавления коментария в базу
    '''
    try:
        page = request.META['HTTP_REFERER']
    except KeyError:
        page = False
    if page:
        if request.method == 'POST':
            comment_form = CommentsForm(request.POST)
            if comment_form.is_valid():
                comment_form.save()
                messages.info(request, 'Ваш коментарий успешно отправлен')
                messages.info(
                    request,
                    'После проверки модератором он будет доступен на сайте')
                return HttpResponseRedirect(page)
            else:
                messages.info(request, 'Ошибка добавления коментария')
                messages.info(request, 'Проверьте введенные данные')
                return HttpResponseRedirect(page)
    else:
        messages.info(request, 'Ошибка добавления коментария')
        messages.info(request, 'Проверьте введенные данные')
        return HttpResponseRedirect('/')
Exemplo n.º 2
0
def post_comment(request, article_id):
    article = get_object_or_404(Article, id=article_id)

    if not request.user.is_authenticated:
        return JsonResponse({'code': 2, 'message': '你还没有登陆'})

    if request.method == "POST":
        form = CommentsForm(request.POST)
        if form.is_valid():
            comment = form.save(False)
            comment.article = article
            if request.POST['parent'] != '-1':
                parent = Comment.objects.get(id=request.POST['parent'])
                comment.parent = parent
            comment.author = request.user
            comment.save()
            comment.pub_time = timezone.localtime(comment.pub_time)
            res = {
                'code': 1,
                'message': '评论成功',
                'cur_comment': {
                    'cid': comment.id,
                    'ctext': comment.text,
                    'ctime': comment.pub_time.strftime('%Y-%m-%d %H:%M'),
                    'cparent': comment.parent_id,
                    'uid': comment.author.id,
                    'uname': comment.author.username,
                },
            }
            return JsonResponse(res)
        else:
            return JsonResponse({'code': 0, 'message': '数据验证错误,评论失败'})

    return JsonResponse({'code': 0, 'message': 'method错误,评论失败'})