我想添加一些验证,如果用户试图注册或登录,他必须通过一些限制。请帮帮我,因为我刚到django 型号.py
class RegisterData(models.Model):
first_name = models.CharField(max_length=20)
last_name = models.CharField(max_length=20)
GENDER_CHOICE =(
('Male','Male'),
('Female', 'Female')
)
gender = models.CharField(max_length=10, choices=GENDER_CHOICE)
username = models.CharField(max_length=20)
password1 = models.CharField(max_length=20)
password2 = models.CharField(max_length=20)
email = models.EmailField(max_length=40)
mobile = models.BigIntegerField()
dob = models.DateField()
表单.py
我以前写这样的表格而不是模型表格。如果有人试图注册,那么他必须遵守一些规则,如密码字符和长度,用户名应该是正确的
登记表
class RegisterForm(forms.Form):
first_name = forms.CharField(
label='First name ',
widget=forms.TextInput(
attrs={
'class':'form-control',
'placeholder':'Enter your first name'
}
)
)
last_name = forms.CharField(
label='Last name ',
widget=forms.TextInput(
attrs={
'class': 'form-control',
'placeholder': 'Enter your last name'
}
)
)
GENDER_CHOICE = (
('male', 'Male'),
('female', 'Female')
)
gender = forms.ChoiceField(
widget=forms.RadioSelect(),
choices=GENDER_CHOICE
)
username = forms.CharField(
label='User name ',
widget=forms.TextInput(
attrs={
'class':'form-control',
'placeholder':'Enter your user name'
}
)
)
password1 = forms.CharField(
label='Password ',
widget=forms.PasswordInput(
attrs={
'class':'form-control',
'placeholder':'Enter your password'
}
)
)
password2 = forms.CharField(
label='Password',
widget=forms.PasswordInput(
attrs={
'class':'form-control',
'placeholder':'Re-enter your password'
}
)
)
email = forms.EmailField(
label='Email-id',
widget=forms.EmailInput(
attrs={
'class':'form-control',
'placeholder':'Enter your email id'
}
)
)
mobile = forms.IntegerField(
label='Mobile ',
widget=forms.NumberInput(
attrs={
'class':'form-control',
'placeholder':'Enter your mobile number'
}
)
)
dob = forms.CharField(
label='DOB ',
widget=forms.DateInput(
attrs={
'class':'form-control',
'placeholder':'Enter your date of birth'
}
)
)
登录窗体
class LoginForm(forms.Form):
username = forms.CharField(
label='User name ',
widget=forms.TextInput(
attrs={
'class': 'form-control',
'placeholder': 'Enter your user name'
}
)
)
password1 = forms.CharField(
label='Password ',
widget=forms.PasswordInput(
attrs={
'class': 'form-control',
'placeholder': 'Enter your password'
}
)
)
视图.py
def regview(request):
if request.method == 'POST':
rform = RegisterForm(request.POST)
if rform.is_valid():
first_name = request.POST.get('first_name','')
last_name = request.POST.get('last_name', '')
gender = rform.cleaned_data.get('gender', '')
username = request.POST.get('username', '')
password1 = request.POST.get('password1', '')
password2 = request.POST.get('password2', '')
email = request.POST.get('email', '')
mobile = request.POST.get('mobile', '')
dob = rform.cleaned_data.get('dob', '')
data = RegisterData(
first_name=first_name,
last_name=last_name,
gender=gender,
username=username,
password1=password1,
password2=password2,
email=email,
mobile=mobile,
dob=dob
)
data.save()
rform = RegisterForm()
lform = LoginForm()
return render(request, 'reg.html', {'rform':rform, 'lform': lform})
if request.method == 'POST':
lform = LoginForm(request.POST)
if lform.is_valid():
username = request.POST.get('username','')
password1 = request.POST.get('password1','')
user = RegisterData.objects.filter(username=username)
pwd = RegisterData.objects.filter(password1=password1)
if user and pwd:
return redirect('/ask')
else:
return HttpResponse('Invalid credentials')
# lform = LoginForm()
# return render(request,'reg.html',{'lform':lform})
else:
rform = RegisterForm()
lform = LoginForm()
return render(request,'reg.html',{'rform':rform ,'lform':lform})
如果您想使用Djangopassword validation中的默认用户窗体,另一个选项是:
相关问题 更多 >
编程相关推荐