简体   繁体   English

分配前引用的 UnboundLocalError 局部变量“上下文”

[英]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我在保存几乎相同类型的表单时遇到此错误(分配前引用了 UnboundLocalError 局部变量 'context'),其他工作正常,但这个不是并且显示错误

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): def 离合器Detail_add(请求):

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.请帮我解决这个错误,我是 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:(我有另一个 100% 相同的表单代码,它工作正常,但这个代码给了我一个错误,我被困在它上面:(

The reason you get this error is because your function contains a codepath where you use the context variable, without defining the variable first.您收到此错误的原因是您的 function 包含一个代码路径,您在其中使用context变量,而没有先定义变量。 That is for example the case when you make a POST request, but the form.is_valid() check fails.例如,当您发出 POST 请求,但form.is_valid()检查失败时就是这种情况。 In that case, the codeflow will call the render(..) function, but you did not define the context variable.在这种情况下,代码流将调用render(..) function,但您没有定义context变量。

The smallest fix would probably be to move the definition of the context variable outside the else statement:最小的修复可能是将context变量的定义移到else语句之外

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()
    context = {
        '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.如果方法是 POST 并且form.is_valid()返回False ,或者form.save()引发异常,则会引发此错误,因为您从未声明过上下文变量。

You also probably don't need to do try: except: on your form.save() call as the form is valid at that point.您也可能不需要try: except:在您的form.save()调用上,因为该表单在那时是有效的。

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定义在第 12 行

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

this only runs when the else: statement is triggered, so when这仅在 else: 语句被触发时运行,所以当

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

context would not be defined if the else statement was not triggered Cheers!如果没有触发 else 语句,则不会定义上下文 干杯!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 UnboundLocalError at / local variable 'context' 在赋值之前引用 - UnboundLocalError at / local variable 'context' referenced before assignment 赋值前引用的 UnboundLocalError 局部变量 'context' Django - UnboundLocalError local variable 'context' referenced before assignment Django UnboundLocalError:分配前已引用局部变量“ truebomb” - UnboundLocalError: local variable 'truebomb' referenced before assignment UnboundLocalError:分配前已引用局部变量“ cur” - UnboundLocalError: local variable 'cur' referenced before assignment UnboundLocalError:分配前已引用局部变量“ Counter” - UnboundLocalError: local variable 'Counter' referenced before assignment UnBoundLocalError:赋值之前引用的局部变量(Python) - UnBoundLocalError: local variable referenced before assignment (Python) UnboundLocalError:分配前已引用局部变量“ strdate” - UnboundLocalError: local variable 'strdate' referenced before assignment UnboundLocalError:赋值之前引用了局部变量“ key” - UnboundLocalError: local variable 'key' referenced before assignment unboundLocalError:赋值前引用了局部变量“loopback” - unboundLocalError: local variable 'loopback' referenced before assignment UnboundLocalError:分配前已引用局部变量“ endProgram” - UnboundLocalError: local variable 'endProgram' referenced before assignment
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM