我正在尝试使用django表单创建表单。我在输入数据并单击提交时创建了注册表单,它在/ register处返回ValueError,如果我输入了错误数据(例如无效电子邮件),则未显示相应的错误?
def register(request):
if request.method == "POST":
form = Register(request.POST)
if form.is_valid():
email = form.cleaned_data['Email']
User_name=form.cleaned_data['Username']
Password=form.cleaned_data['Password']
Confirm_Password=form.cleaned_data['Confirm_Password']
User.objects.create_user(username=User_name,
password=Password,email=email)
return redirect("register")
else:
form = Register()
return render(request,'register.html',{'form': form})
<!------register.html--->
{% extends 'layout.html' %}
{% block content %}
<div class="box">
<h2>
<center>Register</center>
</h2><br>
<form action='register' method='POST'>
{% csrf_token %}
<div>
<label>Email:</label>
{{ form.Email }}
{{ form.Email.errors }}
</div>
<div>
<label>Username:</label>
{{ form.Username }}
{{ form.Username.errors }}
</div>
<div>
<label>Password:</label>
{{ form.Password }}
</div>
<div>
<label>Confirm Password:</label>
{{ form.Confirm_Password }}
{{ form.Confirm_Password.errors }}
</div>
<input type="Submit" id="lg"/><br>
<center><a href="login" >Already have an account.Login here.</a>
</center>
</form>
</div>
<div>
{% for message in messages%}
<h1>{{message}}</h1>
{% endfor %}
</div>
{% endblock %}
forms.py
from django import forms
from django.contrib.auth.models import User
from django.core.validators import validate_email
class Register(forms.Form):
Email = forms.EmailField(widget=forms.TextInput(attrs=
{"class":"inputvalues"}))
Username = forms.CharField(widget=forms.TextInput(attrs=
{"class":"inputvalues"}))
Password = forms.CharField(widget=forms.PasswordInput(attrs=
({"class":"inputvalues"})))
Confirm_Password = forms.CharField(widget=forms.PasswordInput(attrs=
({"class":"inputvalues"})))
class Meta():
model = User
fields = ['email','username','password']
def clean_Email(self):
try:
validate_email(self.cleaned_data['Email'])
except:
raise forms.ValidationError("Email is not in correct format!")
return self.cleaned_data['Email']
def clean_Username(self):
try:
User.objects.get(username = self.cleaned_data['Username'])
except:
return self.cleaned_data['Username']
raise forms.ValidationError("Username already exist!")
def clean_Confirm_Password(self):
pas=self.cleaned_data['Password']
cpas = self.cleaned_data['Confirm_Password']
if pas != cpas:
raise forms.ValidationError("Password and Confirm Password are
not matching!")
else:
if len(pas) < 8:
raise forms.ValidationError("Password should have atleast
8 character")
if pas.isdigit():
raise forms.ValidationError("Password should not all
numeric")
显示错误消息:/ register中的ValueError 视图blog.views.register没有返回HttpResponse对象。它返回None。
答案 0 :(得分:0)
在views.py
def register(request):
if request.method == "POST":
form = Register(request.POST)
if form.is_valid():
........
return redirect("register")
else:
return render(request,'register.html',{'form': form})
else:
form = Register()
return render(request,'register.html',{'form': form})
在register.html中显示如下错误:
{% if form.errors %}
<div class="alert alert-danger">
{{ form.errors }}
</div>
{% endif %}