Моя форма не возвращает никаких ошибок в Django
Я разработчик Django, и я пытаюсь поместить две формы в один HTML-файл (формы регистрации и входа), но когда я пишу в них недопустимый ввод, они не возвращают никаких ошибок.
views.py:
def log(request):
if request.method == 'POST':
print(request.POST)
if 'email' in request.POST:
regisform = RegisterForms(request.POST)
if regisform.is_valid():
print("is valid")
clean_data = regisform.cleaned_data
user =User.objects.create_user(username=clean_data['user_name'],password=clean_data['password_2'],email=clean_data['email'])
login(request,user)
messages.success(request, "شما با موفقیت ثبت نام شدید." )
Profile.objects.get(user_id = request.user.id).hearts_time = jdatetime.datetime.today()
send_user_notification(user=user, payload={"head":"تبریک", "body":"به سایت ویکی ریاضی حوش آمدید."}, ttl=10)
return redirect('home:homepage')
else:
print("is not valid.")
else:
form = LoginForms(request.POST)
if form.is_valid():
data = form.data
print(data)
print("running")
try:
user = authenticate(request, username = data['username'],password=data['password'])
print(user)
except:
user =authenticate(request, email = data['email'],password=data['password'])
print(user)
if user is not None:
login(request, user)
messages.success(request, "شما با موفقیت وارد شدید.","warning")
else:
print('NOT LOGIN')
return redirect('home:homepage')
else:
print("Naderi")
form = LoginForms()
regisform = RegisterForms()
try:
content = {
'form':form,
'regist':regisform,
}
except:
content = {
'regist':regisform,
}
return render(request, 'accounts/login.html',content)
forms.py:
class RegisterForms(forms.Form):
user_name = forms.CharField(max_length=30, widget=forms.TextInput(attrs={"placeholder":'username'}))
password_1 = forms.CharField(max_length=30,widget=forms.PasswordInput(attrs={'placeholder':'password'}))
password_2 = forms.CharField(max_length=30,widget=forms.PasswordInput(attrs={'placeholder':'Confirm password'}))
email = forms.EmailField(widget=forms.EmailInput(attrs={'placeholder':'Email'}))
captcha = ReCaptchaField(widget=ReCaptchaV2Checkbox)
def clean_password_2(self):
print('func 1 is begining.')
password1 = self.cleaned_data['password_1']
password2 = self.cleaned_data['password_2']
listletter = 'abcdefghijklmnopqrstuvwxyz'
listletterc = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
adad = '1234567890'
print(password1,password2)
if password1 != password2:
raise forms.ValidationError('the passwords are not equal.')
elif len(password2)< 6:
raise forms.ValidationError('Short Password.')
small = 0
cap = 0
ada = 0
for i in range(26):
letter = listletter[i]
capp = listletterc[i]
if letter in password2:
small += 1
if capp in password2:
cap += 1
for i in range(10):
num = adad[i]
if num in password2:
ada += 1
if small == 0:
raise forms.ValidationError('do not use the small letters.')
if cap == 0:
raise forms.ValidationError('do not use the capital letters.')
if ada == 0:
raise forms.ValidationError('do not use numbers.')
def clean_user_name(self):
print('un')
usern = self.cleaned_data['user_name']
print(usern)
if User.objects.filter(username = usern).exists():
raise forms.ValidationError('Please choose another username.')
и формы входа в систему:
class LoginForms(forms.Form):
username = forms.CharField(max_length=30,widget=forms.TextInput(attrs={'class':'username','placeholder':'Username'}))
password = forms.CharField(max_length=30, widget=forms.PasswordInput(attrs={'class':'password','placeholder':'Password'}))
captcha = ReCaptchaField(widget=ReCaptchaV2Checkbox)
о html-файле:
В чем проблема? В каком файле? Пожалуйста, ответьте быстро.
Я использую MySql для базы данных и Django 4.2.6.