Local Variable 'user_form' referenced before assignment

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

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})

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:

确定