赋值前引用了局部变量“response_data”,表单只适用于一个页面,但现在我再次尝试使用该表单,就会弹出此错误

2024-04-25 06:05:59 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个很好的评论表。所以我决定在我的网页上采用这个评论形式。但赋值出现之前引用的局部变量“response_data”

这是我的回溯

Traceback:
File "/env/local/lib/python2.7/site-packages/django/core/handlers/base.py" in get_response
  132.                     response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/env/local/lib/python2.7/site-packages/django/views/decorators/csrf.py" in wrapped_view
  58.         return view_func(*args, **kwargs)
File "/comments/views.py" in comment_create_view
  49.           return JsonResponse(response_data)

Exception Type: UnboundLocalError at /comment/create/
Exception Value: local variable 'response_data' referenced before assignment

我相信回溯告诉我表单有一些语法错误,我不明白,因为同一个表单适用于其他页面。在

目前我有

^{pr2}$

这是我的评论创建视图的一部分

@csrf_exempt
def comment_create_view(request):
    if request.method == "POST" and request.user.is_authenticated():
        parent_id = request.POST.get('parent_id')
        post_id = request.POST.get("post_id")
        origin_path = request.POST.get("origin_path")
        try:
            post = Post.objects.get(id=post_id)
        except:
            response_dat = {"code":400,"message":"Post does not exists"}
            return JsonResponse(response_data)


        parent_comment = None
        if parent_id is not None:
            try:
                parent_comment = Comment.objects.get(id=parent_id)
            except:
                parent_comment = None

            if parent_comment is not None and parent_comment.post is not None:
                post = parent_comment.post

        form = CommentForm(data=request.POST)
        if form.is_valid():
            comment_text = form.cleaned_data['comment']
            if parent_comment is not None:
                # parent comments exists
                new_comment = Comment.objects.create_comment(
                    user=MyProfile.objects.get(user=request.user),
                    path=parent_comment.get_origin, 
                    text=comment_text,
                    post = post,
                    parent=parent_comment
                    )
                affected_users = parent_comment.get_affected_users()
                notify.send(MyProfile.objects.get(user=request.user),
                    action=new_comment,
                    target=parent_comment, 
                    recipient=parent_comment.user, 
                    affected_users=affected_users,
                    verb='leave')
                hidden_data = {
                    "post_id" : post.id,
                    "origin_path" : request.get_full_path,
                    "parent_id" : parent_comment.id
                }
                comment_form = CommentForm(hidden_data=hidden_data)
                html = render_to_string('main/child_comment.html', {'comment': [new_comment], 
                                        'user': request.user, 
                                        'comment_form':comment_form})
                response_data = {"status":200, "message":"comment_stored", 
                                                "comment":html, 
                                                'parent': True, 
                                                'parent_id': parent_comment.id,
                                                'comment_count': parent_comment.comment_count()}
                return JsonResponse(response_data)

在后页,它工作得很好,因为我有这样的

def post(请求,slug):

    post = get_object_or_404(Post, slug=slug)
    post.views += 1  # increment the number of views
    post.save()      # and save it
    profile = post.moderator
    #path = request.get_full_path()
    #comments = Comment.objects.filter(path=path)
    comments_count = Comment.objects.filter(post=post).count()
    comments = post.commented_post.all()
    for c in comments:
            c.get_children()
    hidden_data = {
                "post_id" : post.id,
                "origin_path" : request.get_full_path,
                "parent_id" : None
            }
    comment_form = CommentForm(hidden_data=hidden_data) 
    context_dict = {

        'comments' : comments,
        'comment_form':comment_form,
        'comments_count':comments_count,
        'profile' :profile,

    }
    return render(request, 'main/post.html', context_dict)

Tags: pathformnoneiddatagetobjectsis
1条回答
网友
1楼 · 发布于 2024-04-25 06:05:59

在您的代码中,当您为response指定值时,您将response\u data拼写错误。在

   try:
        post = Post.objects.get(id=post_id)
    except:
        response_dat = {"code":400,"message":"Post does not exists"}
        return JsonResponse(response_data)

应该是

^{pr2}$

相关问题 更多 >