英文:
Local Variable 'user_form' referenced before assignment
问题
这是代码部分的翻译:
我正在使用Django运行一个网站。
这是代码(views.py):
def signup(request):
registered=False
failed_ref=False
wrong_ref=False
if request.method=='POST':
if 'city' in request.POST:
user_form = UserForm(data=request.POST)
profile_form = ProfileForm(data=request.POST)
if user_form.is_valid() and profile_form.is_valid():
user = user_form.save()
user.set_password(user.password)
user.save()
profile = profile_form.save(commit=False)
profile.user = user
try:
ref_con = profile.referral_contact
if ref_con == profile.mobile_no:
failed_ref=True
elif ref_con == Profile.objects.get(mobile_no=ref_con).mobile_no:
prof = Profile.objects.get(mobile_no=ref_con)
wallet_rec = Wallet.objects.get(profile=prof)
wall = Wallet.objects.get(profile=profile)
registered = True
except Profile.DoesNotExist:
wrong_ref = True
data={'registered':registered,'failed_ref':failed_ref,'wrong_ref':wrong_ref}
return JsonResponse(data,safe=False)
else:
user_form=UserForm()
profile_form=ProfileForm()
return JsonResponse({'user_form':user_form,'profile_form':profile_form,'registered':registered,
'failed_ref':failed_ref,'wrong_ref':wrong_ref})
希望这对你有帮助。如果你有任何问题,请随时提出。
英文:
I am running a website using Django .
Here is the code (views.py):
def signup(request):
registered=False
failed_ref=False
wrong_ref=False
if request.method=='POST':
if 'city' in request.POST:
user_form = UserForm(data=request.POST)
profile_form = ProfileForm(data=request.POST)
if user_form.is_valid() and profile_form.is_valid():
user = user_form.save()
user.set_password(user.password)
user.save()
profile = profile_form.save(commit=False)
profile.user = user
try:
ref_con = profile.referral_contact
if ref_con == profile.mobile_no:
failed_ref=True
elif ref_con == Profile.objects.get(mobile_no=ref_con).mobile_no:
prof = Profile.objects.get(mobile_no=ref_con)
wallet_rec = Wallet.objects.get(profile=prof)
wall = Wallet.objects.get(profile=profile)
registered = True
except Profile.DoesNotExist:
wrong_ref = True
data={'registered':registered,'failed_ref':failed_ref,'wrong_ref':wrong_ref}
return JsonResponse(data,safe=False)
else:
user_form=UserForm()
profile_form=ProfileForm()
return JsonResponse({'user_form':user_form,'profile_form':profile_form,'registered':registered,
'failed_ref':failed_ref,'wrong_ref':wrong_ref})
For this, i need to get response in JSON . When i run , i am getting error "local variable 'user_form' referenced before assignment".What change i need to do ?. I am bit confused.
答案1
得分: 2
以下是您要翻译的部分:
这个条件必须失败
if 'city' in request.POST:
因此
user_form = UserForm(data=request.POST) # 是无法到达的代码
因此 user_form
保持未定义。
导致错误 "local variable 'user_form' referenced before assignment"
可能的解决方案:
def signup(request):
registered=False
failed_ref=False
wrong_ref=False
if request.method=='POST':
# 在条件之前定义变量
#
user_form = UserForm(data=request.POST)
profile_form = ProfileForm(data=request.POST)
if 'city' in request.POST:
if user_form.is_valid() and profile_form.is_valid():
user = user_form.save()
user.set_password(user.password)
user.save()
profile = profile_form.save(commit=False)
profile.user = user
try:
ref_con = profile.referral_contact
if ref_con == profile.mobile_no:
failed_ref=True
elif ref_con == Profile.objects.get(mobile_no=ref_con).mobile_no:
prof = Profile.objects.get(mobile_no=ref_con)
wallet_rec = Wallet.objects.get(profile=prof)
wall = Wallet.objects.get(profile=profile)
registered = True
except Profile.DoesNotExist:
wrong_ref = True
data={'registered':registered,'failed_ref':failed_ref,'wrong_ref':wrong_ref}
return JsonResponse(data,safe=False)
else:
user_form=UserForm()
profile_form=ProfileForm()
return JsonResponse({'user_form':user_form,'profile_form':profile_form,'registered':registered,
'failed_ref':failed_ref,'wrong_ref':wrong_ref})
英文:
This condition must be failing
if 'city' in request.POST:
hence
user_form = UserForm(data=request.POST) # is an unreachable code
hence user_form
remains undefined.
Resulting in error "local variable 'user_form' referenced before assignment"
Possible solution:
def signup(request):
registered=False
failed_ref=False
wrong_ref=False
if request.method=='POST':
# Defined the variables before the condition
#
user_form = UserForm(data=request.POST)
profile_form = ProfileForm(data=request.POST)
if 'city' in request.POST:
if user_form.is_valid() and profile_form.is_valid():
user = user_form.save()
user.set_password(user.password)
user.save()
profile = profile_form.save(commit=False)
profile.user = user
try:
ref_con = profile.referral_contact
if ref_con == profile.mobile_no:
failed_ref=True
elif ref_con == Profile.objects.get(mobile_no=ref_con).mobile_no:
prof = Profile.objects.get(mobile_no=ref_con)
wallet_rec = Wallet.objects.get(profile=prof)
wall = Wallet.objects.get(profile=profile)
registered = True
except Profile.DoesNotExist:
wrong_ref = True
data={'registered':registered,'failed_ref':failed_ref,'wrong_ref':wrong_ref}
return JsonResponse(data,safe=False)
else:
user_form=UserForm()
profile_form=ProfileForm()
return JsonResponse({'user_form':user_form,'profile_form':profile_form,'registered':registered,
'failed_ref':failed_ref,'wrong_ref':wrong_ref})
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论