简体   繁体   中英

UnboundLocalError local variable 'context' referenced before assignment

I am stuck at this error(UnboundLocalError local variable 'context' referenced before assignment) while saving form of almost same type other are working fine but this one is not and showing error

def clutchDetail(request):

clutchDetail = ClutchDetail.objects.all()
context = {'title': 'Clutch Detail',
           'active': 'active',
           'clutchDetail': clutchDetail,
           }
return render(request, 'breedingRecApp/clutch_detail.html', context)

def clutchDetail_add(request):

if request.method == "POST":
    form = ClutchDetail_AddModelForm(request.POST or None)
    if form.is_valid():
        try:
            form.save()
            return redirect('breedingRecApp:clutch_detail')

        except:
            pass
else:
    form = ClutchDetail_AddModelForm()
    context = {'title': 'Species Detail Add',
               'active': 'active',
               'model': ClutchDetail,
               'form': form,
               }
return render(request, 'breedingRecApp/clutch_detail_add.html', context)

Please help me to fix this error I am newbie to Django. I've an other form code which 100% same that is working fine but this one gives me an error I am stuck at it:(

The reason you get this error is because your function contains a codepath where you use the context variable, without defining the variable first. That is for example the case when you make a POST request, but the form.is_valid() check fails. In that case, the codeflow will call the render(..) function, but you did not define the context variable.

The smallest fix would probably be to move the definition of the context variable outside the else statement:

def clutchDetail_add(request):
    if request.method == 'POST':
        form = ClutchDetail_AddModelForm(request.POST)
        if form.is_valid():
            form.save()
            return redirect('breedingRecApp:clutch_detail')
    else:
        form = ClutchDetail_AddModelForm()
     = {
        'title': 'Species Detail Add',
        'active': 'active',
        'model': ClutchDetail,
        'form': form,
    }
    return render(request, 'breedingRecApp/clutch_detail_add.html', context)

In the case that the method is a POST and the form.is_valid() returns False , or form.save() raises an exception this error is raised as you never declared the context variable.

You also probably don't need to do try: except: on your form.save() call as the form is valid at that point.

def clutchDetail(request):

    clutchDetail = ClutchDetail.objects.all()
    context = {'title': 'Clutch Detail',
           'active': 'active',
           'clutchDetail': clutchDetail,
           }
    return render(request, 'breedingRecApp/clutch_detail.html', context)

def clutchDetail_add(request):

    if request.method == "POST":
        form = ClutchDetail_AddModelForm(request.POST or None)
        if form.is_valid():
            form.save()
            return redirect('breedingRecApp:clutch_detail')
    else:
        form = ClutchDetail_AddModelForm()

    context = {'title': 'Species Detail Add',
               'active': 'active',
               'model': ClutchDetail,
               'form': form,
    }
    return render(request, 'breedingRecApp/clutch_detail_add.html', context)

Talking about the second code block

The only place

context

is defined is on line 12

context = {'title': 'Species Detail Add',

this only runs when the else: statement is triggered, so when

return render(request, 'breedingRecApp/clutch_detail_add.html', context)

context would not be defined if the else statement was not triggered Cheers!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM