当用户尝试使用错误信息登录/注册时,我试图显示错误
我有这些代码,但错误没有显示在浏览器
我写了这段代码,通过寻找另一个项目,我做了,当我在学习Django,但现在它不工作!
这是我项目的代码
views.py:
def loginPage(request):
if request.user.is_authenticated:
return redirect('/')
form = loginForm(request.POST or None)
if form.is_valid():
username = form.cleaned_data.get('username')
password = form.cleaned_data.get('password')
user = authenticate(request, username=username, password=password)
if user is not None:
login(request, user)
return redirect('/')
content = {
"form": loginForm,
"title": "Login",
}
return render(request, 'login.html', content)
#---------------------------------------------
def registerPage(request):
if request.user.is_authenticated:
return redirect('/')
form = registerForm(request.POST or None)
content = {
"form": registerForm,
"title": "Register",
}
if form.is_valid():
username = form.cleaned_data.get('username')
email = form.cleaned_data.get('email')
password = form.cleaned_data.get('password')
password2 = form.cleaned_data.get('password2')
User.objects.create_user(username=username, email=email, password=password)
return redirect('/')
return render(request, 'register.html', content)
forms.py:
class loginForm(forms.Form):
username = forms.CharField(widget=forms.TextInput(attrs={
"class": "form-control",
}))
password = forms.CharField(widget=forms.PasswordInput(attrs={
"class": "form-control",
}))
class registerForm(forms.Form):
username = forms.CharField(widget=forms.TextInput(attrs={
"class": "form-control",
}))
email = forms.EmailField(widget=forms.EmailInput(attrs={
"class": "form-control",
}))
password = forms.CharField(widget=forms.PasswordInput(attrs={
"class": "form-control",
}))
password2 = forms.CharField(widget=forms.PasswordInput(attrs={
"class": "form-control",
}), label='Password Confirm')
def clean_username(self):
user_name = self.cleaned_data.get("username")
user_exists = User.objects.filter(username=user_name).exists()
if user_exists:
raise forms.ValidationError("Username is in user")
return user_name
def clean_email(self):
email = self.cleaned_data.get('email')
if User.objects.filter(email=email).exists():
raise forms.ValidationError('Email is in use!!')
return email
def clean_password2(self):
pass1 = self.cleaned_data.get('password')
pass2 = self.cleaned_data.get('password2')
if pass1 != pass2:
raise forms.ValidationError('Passwords does not match')
return pass2
html(它是用于注册,但登录是相同的):
<div class="container">
<div class="row justify-content-center">
<h3 class="text-center">{{ title }}</h3>
<hr>
<div class="col-md-4">
<form method="post">
{% csrf_token %}
{% for error in form.username.errors %}
<p style="color: red">{{ error }}</p>
{% endfor %}
{% for error in form.email.errors %}
<p class="bg-danger text-light">{{ error }}</p>
{% endfor %}
{% for error in form.password.errors %}
<p class="bg-danger text-light">{{ error }}</p>
{% endfor %}
{% for error in form.password2.errors %}
<p class="bg-danger text-light">{{ error }}</p>
{% endfor %}
<label for="{{ form.username.id_for_label }}">{{ form.username.label }}</label>
{{ form.username }}
<br>
<label for="{{ form.email.id_for_email }}">{{ form.email.label }}</label>
{{ form.email }}
<br>
<label for="{{ form.password.id_for_label }}">{{ form.password.label }}</label>
{{ form.password }}
<br>
<label for="{{ form.password2.id_for_label }}">{{ form.password2.label }}</label>
{{ form.password2 }}
<hr>
<div class="d-grid gap-2">
<button type="submit" class="btn btn-outline-success">Login</button>
</div>
</form>
</div>
</div>
</div>
您添加到您的上下文未绑定类loginForm
-参见表单API
使用form = registerForm(request.POST)
和form.is_valid()
的绑定形式
所以在你的视图中把它改成:
"form": loginForm
to"form": form
"form": registerForm
to"form": form