donkeyboy72
donkeyboy72

Reputation: 1943

Django No local variable error

I have a problem with one of my functions . I receive this error when I submit a post form without any value.

 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),})

I think I understand what causing the problem because in my views. I have a picture variable that retrieves all my pictures and if their no value coming from POST . I get this error .

I'm been searching and trying for solutions on how to handle if variable is None and I haven't been successful.

Here are the solutions I tried from all the resources i gathered

If board is None , assign it an empty value.

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

How can I combat the error when their is no value toward my picture variable?

Upvotes: 0

Views: 68

Answers (3)

Amyth
Amyth

Reputation: 32949

Following is the line that causes the error:

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

Reason being that the return statement is not under the if condition so it tries returning the picture variable under all circumstances. So according to your code:

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 the if forms.is_valid() condition fails, you are still trying to return picture variable which is undefined. You need to make sure you define the variable before accessing it.

There are a plenty of ways you can do this. For Example:

Example 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),})

Example 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),})

Example 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),})

or put all the variables in a dictionary as Aldarund suggested and them pass the dictionary to the context.

Upvotes: 2

catherine
catherine

Reputation: 22808

[..........]
    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),
        })
[..........]

Upvotes: 2

Aldarund
Aldarund

Reputation: 17621

You will get this error when your form doesn't validate ( is_valid() return false), so in this case you don't have picture defined at all. You can just create a context dictionary at begin and then put there variables you want. Something like this

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

Upvotes: 2

Related Questions