如果用户尝试注册或登录他必须经历一些限制,我想添加一些验证。请帮助我某人,因为我是django的新手 models.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()
forms.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'
}
)
)
loginform
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'
}
)
)
views.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})
答案 0 :(得分:0)
# you can use clean function in your forms.py class
For Example
def clean(self):
cleaned_data = super().clean()
username = cleaned_data.get("username")
password = cleaned_data.get("password")
if len(username)<=8:
raise forms.ValidationError(
"Username field length should be more than 8 characters"
)
return cleaned_data
答案 1 :(得分:0)
如果要使用Django password validation中的默认用户表单,请使用另一个选项:
AUTH_PASSWORD_VALIDATORS = [
{
'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator',
'OPTIONS': {
'min_length': 8,
}
},
]