Local Variable 'user_form' referenced before assignment

huangapple go评论115阅读模式
英文:

Local Variable 'user_form' referenced before assignment

问题

这是代码部分的翻译:

  1. 我正在使用Django运行一个网站
  2. 这是代码views.py):
  3. def signup(request):
  4. registered=False
  5. failed_ref=False
  6. wrong_ref=False
  7. if request.method=='POST':
  8. if 'city' in request.POST:
  9. user_form = UserForm(data=request.POST)
  10. profile_form = ProfileForm(data=request.POST)
  11. if user_form.is_valid() and profile_form.is_valid():
  12. user = user_form.save()
  13. user.set_password(user.password)
  14. user.save()
  15. profile = profile_form.save(commit=False)
  16. profile.user = user
  17. try:
  18. ref_con = profile.referral_contact
  19. if ref_con == profile.mobile_no:
  20. failed_ref=True
  21. elif ref_con == Profile.objects.get(mobile_no=ref_con).mobile_no:
  22. prof = Profile.objects.get(mobile_no=ref_con)
  23. wallet_rec = Wallet.objects.get(profile=prof)
  24. wall = Wallet.objects.get(profile=profile)
  25. registered = True
  26. except Profile.DoesNotExist:
  27. wrong_ref = True
  28. data={'registered':registered,'failed_ref':failed_ref,'wrong_ref':wrong_ref}
  29. return JsonResponse(data,safe=False)
  30. else:
  31. user_form=UserForm()
  32. profile_form=ProfileForm()
  33. return JsonResponse({'user_form':user_form,'profile_form':profile_form,'registered':registered,
  34. 'failed_ref':failed_ref,'wrong_ref':wrong_ref})

希望这对你有帮助。如果你有任何问题,请随时提出。

英文:

I am running a website using Django .

Here is the code (views.py):

  1. def signup(request):
  2. registered=False
  3. failed_ref=False
  4. wrong_ref=False
  5. if request.method=='POST':
  6. if 'city' in request.POST:
  7. user_form = UserForm(data=request.POST)
  8. profile_form = ProfileForm(data=request.POST)
  9. if user_form.is_valid() and profile_form.is_valid():
  10. user = user_form.save()
  11. user.set_password(user.password)
  12. user.save()
  13. profile = profile_form.save(commit=False)
  14. profile.user = user
  15. try:
  16. ref_con = profile.referral_contact
  17. if ref_con == profile.mobile_no:
  18. failed_ref=True
  19. elif ref_con == Profile.objects.get(mobile_no=ref_con).mobile_no:
  20. prof = Profile.objects.get(mobile_no=ref_con)
  21. wallet_rec = Wallet.objects.get(profile=prof)
  22. wall = Wallet.objects.get(profile=profile)
  23. registered = True
  24. except Profile.DoesNotExist:
  25. wrong_ref = True
  26. data={'registered':registered,'failed_ref':failed_ref,'wrong_ref':wrong_ref}
  27. return JsonResponse(data,safe=False)
  28. else:
  29. user_form=UserForm()
  30. profile_form=ProfileForm()
  31. return JsonResponse({'user_form':user_form,'profile_form':profile_form,'registered':registered,
  32. '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

以下是您要翻译的部分:

这个条件必须失败

  1. if 'city' in request.POST:

因此

  1. user_form = UserForm(data=request.POST) # 是无法到达的代码

因此 user_form 保持未定义。

导致错误 "local variable 'user_form' referenced before assignment"

可能的解决方案:

  1. def signup(request):
  2. registered=False
  3. failed_ref=False
  4. wrong_ref=False
  5. if request.method=='POST':
  6. # 在条件之前定义变量
  7. #
  8. user_form = UserForm(data=request.POST)
  9. profile_form = ProfileForm(data=request.POST)
  10. if 'city' in request.POST:
  11. if user_form.is_valid() and profile_form.is_valid():
  12. user = user_form.save()
  13. user.set_password(user.password)
  14. user.save()
  15. profile = profile_form.save(commit=False)
  16. profile.user = user
  17. try:
  18. ref_con = profile.referral_contact
  19. if ref_con == profile.mobile_no:
  20. failed_ref=True
  21. elif ref_con == Profile.objects.get(mobile_no=ref_con).mobile_no:
  22. prof = Profile.objects.get(mobile_no=ref_con)
  23. wallet_rec = Wallet.objects.get(profile=prof)
  24. wall = Wallet.objects.get(profile=profile)
  25. registered = True
  26. except Profile.DoesNotExist:
  27. wrong_ref = True
  28. data={'registered':registered,'failed_ref':failed_ref,'wrong_ref':wrong_ref}
  29. return JsonResponse(data,safe=False)
  30. else:
  31. user_form=UserForm()
  32. profile_form=ProfileForm()
  33. return JsonResponse({'user_form':user_form,'profile_form':profile_form,'registered':registered,
  34. 'failed_ref':failed_ref,'wrong_ref':wrong_ref})
英文:

This condition must be failing

  1. if 'city' in request.POST:

hence

  1. 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:

  1. def signup(request):
  2. registered=False
  3. failed_ref=False
  4. wrong_ref=False
  5. if request.method=='POST':
  6. # Defined the variables before the condition
  7. #
  8. user_form = UserForm(data=request.POST)
  9. profile_form = ProfileForm(data=request.POST)
  10. if 'city' in request.POST:
  11. if user_form.is_valid() and profile_form.is_valid():
  12. user = user_form.save()
  13. user.set_password(user.password)
  14. user.save()
  15. profile = profile_form.save(commit=False)
  16. profile.user = user
  17. try:
  18. ref_con = profile.referral_contact
  19. if ref_con == profile.mobile_no:
  20. failed_ref=True
  21. elif ref_con == Profile.objects.get(mobile_no=ref_con).mobile_no:
  22. prof = Profile.objects.get(mobile_no=ref_con)
  23. wallet_rec = Wallet.objects.get(profile=prof)
  24. wall = Wallet.objects.get(profile=profile)
  25. registered = True
  26. except Profile.DoesNotExist:
  27. wrong_ref = True
  28. data={'registered':registered,'failed_ref':failed_ref,'wrong_ref':wrong_ref}
  29. return JsonResponse(data,safe=False)
  30. else:
  31. user_form=UserForm()
  32. profile_form=ProfileForm()
  33. return JsonResponse({'user_form':user_form,'profile_form':profile_form,'registered':registered,
  34. 'failed_ref':failed_ref,'wrong_ref':wrong_ref})

huangapple
  • 本文由 发表于 2020年1月3日 17:17:50
  • 转载请务必保留本文链接:https://go.coder-hub.com/59575858.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定