如何解决django:UnboundLocalError [英] how to solve django: UnboundLocalError
本文介绍了如何解决django:UnboundLocalError的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
环境:
请求方法:GET
请求URL:http://127.0.0.1:8000/budget/show/
Django版本:1.2.5
Python版本:2.7.1
安装的应用程序:
['django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
' django.contrib.sites',
'django.contrib.messages',
'mysite.bug']
安装的中间件:
('django.middleware.common.CommonMiddleware' ,
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware')
追溯:
文件/usr/lib/pymodules/python2.7/django/core/handlers/base .pyin get_response 100. response = callback(request,* callback_args,** callback_kwargs)
文件/home/nimesh/budman/mysite/../mysite/bug/views.py如图252所示。
return render_to_response('budgetfinalform3.html',{'account_number':c,'period':d,'month':e,'year':f,'formsetlist':formset,'child':child} ,context_instance = RequestContext(request))
异常类型:/ budget / show /
中的UnboundLocalError异常值:赋值前引用的局部变量formset
plz帮助我删除此错误。
def show(request):
b = request.session [s1]
c = request.session [s2]
d = request.session [s3]
a = account_period.objects.filter(ye ar = d).values('id')
e = account_period.objects.filter(year = d).values('month')
f = account_period.objects.filter(id = a).values ('year')
try:
child = account_tab.objects.filter(parent_account_number = c).values('account_number')
noofchild = account_tab.objects.filter parent_account_number = c).count()
除了account_tab.DoesNotExist:
noofchild = 0
如果noofchild!= 0:
formsetlist = []
for i in range(0,noofchild):
formsetlist.append(formset_factory(bu,extra = b))
如果request.method =='POST':
formsetlist2.append(formsetlist [i](request.POST))
如果formsetlist2 [i] .is_valid():
j = 0
for formsetlist2 [ i] .forms:
z = account_tab.objects.get(account_number = child [i:(i + 1)])
x = form.cleaned_data ['value']
y = account_period.objects .get(id = a [j:(j + 1)])
try:
uip = budget.objects.get(account_no = child [i:(i + 1)],account_period = a [j:(j + 1)])
如果uip。 budget_amount!= x:
uip.budget_amount = x
uip.save()
除了budget.DoesNotExist:
w = budget (account_no = z,account_period = y,budget_amount = x,created_by_login ='me')
w.save()
j = j + 1
pass
return HttpResponse ')
else:
返回render_to_response('budgetfinalform3.html',{'account_number':c,'period':d,'month':e,'year ':f,'formsetlist':formset,'child':child},context_instance = RequestContext(request))
#return HttpResponse(mes)
模板代码是
< html>
< head>
< title> BUDGET< / title>
< / head>
< body>
< h1 BUDGET MANAGEMENTS< / h1>
< h2>您的帐号为:{{account_number}}。< / h2> < h2>您选择{{period}} {{month}}作为句点< / h2>
{%if form.errors%}
< p style =color:red;>
请更正以下错误{{form.errors | pluralize}}。
< / p>
{%endif%}
< form action =。方法=post> {%csrf_token%}
{form for formsetlist%中的formset}
{{formset.management_form}}
{{child}}
< table>
{form for formset.forms%}
{%ifloop.counter == 0%}
{{child.1}}
{% endif%}
{{form}}
{%endfor%}
< / table>
{%endfor%}
< input type =submitvalue =Submit>
< / form>
< / body>
< / html>
解决方案
return render_to_response ('budgetfinalform3.html',{'account_number':c,'period':d,'month':e,'year':f,'formsetlist':formset,'child':child},context_instance = RequestContext )
使用 formset
但它看起来/听起来像你的意思 formsetlist
:
return render_to_response('budgetfinalform3.html ',{'account_number':c,'period':d,'month':e,'year':f,'formsetlist':formsetlist,'child':child},context_instance = RequestContext(request))
I have the given view and template code, when i try to run this code i get the following errors.when i searched about this type of error,i found that it happens when a variable and function is given the same name,but could't correct it in my code.
Environment:
Request Method: GET
Request URL: http://127.0.0.1:8000/budget/show/
Django Version: 1.2.5
Python Version: 2.7.1
Installed Applications:
['django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.sites',
'django.contrib.messages',
'mysite.bug']
Installed Middleware:
('django.middleware.common.CommonMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware')
Traceback:
File "/usr/lib/pymodules/python2.7/django/core/handlers/base.py" in get_response 100. response = callback(request, *callback_args, **callback_kwargs)
File "/home/nimesh/budman/mysite/../mysite/bug/views.py" in shown 252.
return render_to_response('budgetfinalform3.html', {'account_number': c,'period':d,'month':e,'year':f,'formsetlist': formset,'child':child},context_instance=RequestContext(request))
Exception Type: UnboundLocalError at /budget/show/
Exception Value: local variable 'formset' referenced before assignment
plz help me to remove this error.
def show (request):
b = request.session["s1"]
c = request.session["s2"]
d = request.session["s3"]
a = account_period.objects.filter(year=d).values('id')
e = account_period.objects.filter(year=d).values('month')
f = account_period.objects.filter(id = a).values('year')
try:
child = account_tab.objects.filter(parent_account_number=c).values('account_number')
noofchild = account_tab.objects.filter(parent_account_number=c).count()
except account_tab.DoesNotExist:
noofchild = 0
if noofchild != 0 :
formsetlist = []
for i in range(0, noofchild):
formsetlist.append(formset_factory(bu, extra=b))
if request.method == 'POST':
formsetlist2.append(formsetlist[i](request.POST))
if formsetlist2[i].is_valid():
j=0
for form in formsetlist2[i].forms:
z = account_tab.objects.get(account_number = child[i:(i+1)])
x = form.cleaned_data['value']
y = account_period.objects.get(id=a[j:(j+1)])
try:
uip = budget.objects.get(account_no = child[i:(i+1)],account_period = a[j:(j+1)])
if uip.budget_amount != x:
uip.budget_amount = x
uip.save()
except budget.DoesNotExist:
w = budget(account_no = z, account_period = y, budget_amount = x, created_by_login = 'me')
w.save()
j=j+1
pass
return HttpResponse('thanks')
else:
return render_to_response('budgetfinalform3.html', {'account_number': c,'period':d,'month':e,'year':f,'formsetlist': formset,'child':child},context_instance=RequestContext(request))
#return HttpResponse(mes)
Template code is
<html>
<head>
<title>BUDGET</title>
</head>
<body>
<h1>BUDGET MANAGEMENTS</h1>
<h2>Your Account Number is : {{ account_number }}.</h2> <h2>You Chose {{ period }} {{month}} as period</h2>
{% if form.errors %}
<p style="color: red;">
Please correct the error{{ form.errors|pluralize }} below.
</p>
{% endif %}
<form action="." method="post">{% csrf_token %}
{% for formset in formsetlist %}
{{ formset.management_form }}
{{child}}
<table>
{% for form in formset.forms %}
{% if forloop.counter == 0 %}
{{child.1}}
{% endif%}
{{ form }}
{% endfor %}
</table>
{% endfor %}
<input type="submit" value="Submit">
</form>
</body>
</html>
解决方案
return render_to_response('budgetfinalform3.html', {'account_number': c,'period':d,'month':e,'year':f,'formsetlist': formset,'child':child},context_instance=RequestContext(request))
Uses formset
but it looks/sounds like you meant formsetlist
:
return render_to_response('budgetfinalform3.html', {'account_number': c,'period':d,'month':e,'year':f,'formsetlist': formsetlist,'child':child},context_instance=RequestContext(request))
这篇关于如何解决django:UnboundLocalError的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文