python-Django UnboundLocalError
我有一个简单的表单集,用于将捐赠者注册到我的数据库。
如果我登陆页面上来自 DB 的现有条目并从表单输入中删除一个值并提交,我会得到一个 UnboundLocalError。
Django Version: 1.3
Exception Type: UnboundLocalError
Exception Value: local variable 'dac' referenced before assignment
views.py
@login_required
def dizimista(request, nr_dizimista):
dizimista = get_object_or_404(CadastroDizimista, pk=nr_dizimista, usuario=request.user)
if request.method == 'POST' and request.POST.get('action') == 'Cancelar':
return HttpResponseRedirect("/lista/")
elif request.method == 'POST' and request.POST.get('action') == 'Salvar':
form = FormCadastroDizimista(request.POST, request.FILES, instance=dizimista)
if form.is_valid():
cadastro = form.save(commit=False)
cadastro.usuario = request.user
cadastro.save()
return HttpResponseRedirect("/lista/")
else:
form = FormCadastroDizimista(instance=dizimista)
dac = CadastroDizimista.objects.get(pk=nr_dizimista).datacadastro
dam = CadastroDizimista.objects.get(pk=nr_dizimista).datamodificado
return render_to_response("cadastro.html", {'dac': dac, 'dam': dam, 'form': form}, context_instance=RequestContext(request))