用户表单
class UserForm(forms.ModelForm):
confirm_password = forms.CharField(label="Confirm Password",widget=forms.PasswordInput(attrs = {'placeholder': 'Confirm Password','class':'required'}))
phone = forms.CharField(max_length = 15,widget = forms.TextInput(attrs = {'placeholder':'Enter mobile no. ','class':'required number'}))
profession = forms.CharField(max_length= 50,widget = forms.Select(choices = PROFESSION_CHOICES,attrs = {'class':'required'}))
email = forms.EmailField(label='Email address',max_length = 75,widget = forms.TextInput(attrs={'placeholder':'Enter a valid email.','class':'required email'}))
sex = forms.CharField(max_length = 20,label="I am :",widget=forms.Select(choices=SEX_CHOICES,attrs = {'class':'required'}))
password = forms.CharField(label="Password",widget=forms.PasswordInput(attrs = {'placeholder': 'Password','class':'required'}))
first_name = forms.CharField(max_length = 50,widget = forms.TextInput(attrs={'placeholder':'Please enter your real name.','class':'required alphabets'}))
last_name = forms.CharField(max_length = 50,widget = forms.TextInput(attrs={'placeholder':'Enter last name.','class':'required alphabets'}))
def clean_first_name(self):
first_name = self.cleaned_data['first_name']
if first_name == '':
raise forms.ValidationError("This field is required.")
def clean_phone(self):
phone = self.cleaned_data['phone']
if phone == '':
raise forms.ValidationError("This field is required.")
def clean_last_name(self):
last_name = self.cleaned_data['last_name']
if last_name == '':
raise forms.ValidationError("This field is required.")
def clean_email(self):
email = self.cleaned_data.get("email")
try:
user = User.objects.get(email = email)
raise forms.ValidationError("Email already in use.")
except User.DoesNotExist:
return email
def clean_profession(self):
profession = self.cleaned_data['profession']
if profession == "":
raise forms.ValidationError("Select a valid option.")
def clean_sex(self):
sex = self.cleaned_data['sex']
if sex == "":
raise forms.ValidationError("Select a valid option.")
def save(self,*args,**kw):
user = super(UserForm,self).save(*args,**kw)
user.set_password(self.cleaned_data.get("password"))
user.first_name = self.cleaned_data.get("first_name")
user.last_name = self.cleaned_data.get("last_name")
user.email = self.cleaned_data.get("email")
user.save()
user.get_profile().phone = self.cleaned_data.get('phone')
user.get_profile().location = self.cleaned_data.get('location')
user.get_profile().profession = self.cleaned_data.get('profession')
user.get_profile().sex = self.cleaned_data.get('sex')
return user
class Meta:
model = User
fields = ('username','email','password','confirm_password','first_name','last_name','sex','phone','profession')
widgets = {
'password': forms.PasswordInput(),
}
用户注册视图
def register_user(request):
if request.POST:
data = request.POST.copy()
data["username"] = 'user'
rform = UserForm(data)
#form = UserProfileForm()
if rform.is_valid():
try:
user = rform.save()
user.username = "user"+str(user.id)
user.save()
user = authenticate(username = user.username,password=user.password)
#register user
login(request,user)
return redirect(index)
except:
print "Unexpected error"
raise
else:
# submit the same form again.
form = LoginForm();
sform = LoginForm()
return render_to_response('register.html',{'rform':rform,'form':form,'sform':sform},context_instance = RequestContext(request))
else:
rform = UserForm()
#form = UserProfileForm()
form = LoginForm()
sform = LoginForm()
return render_to_response('register.html',{'rform':rform,'form':form,'sform':sform},context_instance = RequestContext(request))
错误
IntegrityError at /accounts/register/
auth_user.first_name may not be NULL
怀疑
当我使用普通用户身份验证时,一切正常,但是当我将其与电子邮件身份验证一起使用时,它给了我上述错误,我该如何克服这个错误,请帮助,以及我如何制作电子邮件字段独一无二,如我如何向该字段添加索引,请帮助