继承AbstractUser model 和 UserCreationForm后写的用户注册页面,提交数据总是显示{'password_mismatch': 'The two password fields didn’t match.'}, 查看后发现调用form.is_valid后cleaned_data中password2变为None了。
Models.py
class Users(AbstractUser):
#增加了一个field
bio = models.TextField(verbose_name="Bio", null=True)
自定义的Form:
class MyUserCreationForm(UserCreationForm):
class Meta:
model=models.Users
#其实只修改了model, fields不允许为空,我就直接从父类抄过来了
fields = ("username",)
views.py:
class UserCreation(View):
def get(self,request):
content=homeforms.MyUserCreationForm()
return render(request,'user_create.html',{'form':content})
def post(self,request):
result = homeforms.MyUserCreationForm(data=request.POST)
if result.is_valid():
result.save()
username=result.cleaned_data.get("username")
raw_password=result.cleaned_data.get('password1')
user=authenticate(username=username, password=raw_password)
login(request,user)
return redirect('/book/')
print( str(result.cleaned_data['password1'])+" and "+ str(request.POST.get('password2')))
return render(request, "user_create.html",{'form':result})
444 and : 444
{'password_mismatch': 'The two password fields didn’t match.'}
可以看到request 里确实有password2的值,但是用.cleaned_data['password2']获取就变成Nonel .
各位给看看是哪里的问题?多谢!