Django没有局部变量错误

时间:2013-03-26 09:01:09

标签: django

我的某个功能有问题。当我提交没有任何价值的帖子表格时,我收到此错误。

 local variable 'picture' referenced before assignment

 Traceback:
 File "C:\Python26\Lib\site-packages\django\core\handlers\base.py" in get_response
   111.                         response = callback(request, *callback_args, **callback_kwargs)
 File "C:\o\17\mysite\pet\views.py" in BoardEditor
   260.             return render(request,'boardeditor.html',     {'picture':picture,'board':BoardNameForm(request.user),'boardpicture':BoardPictureForm(request.user),})

我想我理解导致问题的原因,因为在我的观点中。我有一个图片变量,可以检索我的所有图片,如果没有来自POST的值。我收到这个错误。

我一直在寻找解决方案,如果变量是None而且我没有成功,如何处理。

以下是我从我收集的所有资源中尝试的解决方案

如果board为None,则为其指定一个空值。

        if forms.is_valid():
            board = forms.cleaned_data['board']
            if board == None:
                picture = ""

如果错误对我的图片变量没有价值,我该怎么办?

3 个答案:

答案 0 :(得分:2)

当您的表单未验证时(is_valid()返回false),您将收到此错误,因此在这种情况下您根本没有定义图片。 您可以在开始时创建上下文字典,然后将所需的变量放入其中。像这样的东西

 context_data = {}
 if form.is_valid():
   context_data['picture']=Picture.objects.filter(board=boards)
 return render(request,'boardeditor.html',context_data)

答案 1 :(得分:2)

[..........]
    if forms.is_valid():
        board = forms.cleaned_data['board']

        picture = "" //<--add
        if board:
            boards = forms.cleaned_data['board']
            picture = Picture.objects.filter(board=boards)
        return render(request,'boardeditor.html',{ // align with if board:
            'picture':picture, 
            'board':BoardNameForm(request.user), 
            'boardpicture':BoardPictureForm(request.user),
         })
    if formss.is_valid():
        pooh = forms.cleaned_data['board']

        pict = "" //<--add
        if pooh:
            pooh = formss.cleaned_data['board']
            pict = Picture.objects.filter(board=pooh)
        return render(request,'boardeditor.html',{ // align with if pooh:
            'pict':pict, 
            'board':BoardNameForm(request.user), 
            'boardpicture':BoardPictureForm(request.user),
        })
[..........]

答案 2 :(得分:2)

以下是导致错误的行:

return render(request,'boardeditor.html',{'picture':picture,'board':BoardNameForm(request.user),'boardpicture':BoardPictureForm(request.user),})

原因是return语句不在if条件下,所以它尝试在所有情况下返回picture变量。所以根据你的代码:

if forms.is_valid():
    board = forms.cleaned_data['board']
    if board == None:
        picture = ""
        if board:
            boards = forms.cleaned_data['board']
            picture = Picture.objects.filter(board=boards)
return render(request,'boardeditor.html',{'picture':picture,'board':BoardNameForm(request.user),'boardpicture':BoardPictureForm(request.user),})

如果if forms.is_valid()条件失败,您仍在尝试返回未定义的picture变量。您需要确保在访问变量之前定义变量。

有很多方法可以做到这一点。例如:

示例1:

if forms.is_valid():
    board = forms.cleaned_data['board']
        if board:
            boards = forms.cleaned_data['board']
            picture = Picture.objects.filter(board=boards)
else:
    picture = '' # or None or False or Whatever
return render(request,'boardeditor.html',{'picture':picture,'board':BoardNameForm(request.user),'boardpicture':BoardPictureForm(request.user),})

示例2:

picture = '' # or None or False or Whatever
if forms.is_valid():
    board = forms.cleaned_data['board']
        if board:
            boards = forms.cleaned_data['board']
            picture = Picture.objects.filter(board=boards)
return render(request,'boardeditor.html',{'picture':picture,'board':BoardNameForm(request.user),'boardpicture':BoardPictureForm(request.user),})

示例3:

if forms.is_valid():
    board = forms.cleaned_data['board']
        if board:
            boards = forms.cleaned_data['board']
            picture = Picture.objects.filter(board=boards)
    return render(request,'boardeditor.html',{'picture':picture,'board':BoardNameForm(request.user),'boardpicture':BoardPictureForm(request.user),})
else:
    return render(request,'boardeditor.html',{'picture':'','board':BoardNameForm(request.user),'boardpicture':BoardPictureForm(request.user),})

或将所有变量放在Aldarund建议的字典中,然后将字典传递给上下文。