we aren't having a common validation issue. The issue we are having is that 0 validation is being checked. As long as the username has input form will submit as it should. We would like to not have the form submit and just display an error message above the field. Here is the code:
class register_form(forms.ModelForm):
username = forms.CharField(max_length=200, help_text="Username: ")
fname = forms.CharField(max_length=200, help_text="First Name: ")
lname = forms.CharField(max_length=200, help_text="Last Name: ")
email = forms.CharField(max_length=255, help_text="Email: ")
remail = forms.CharField(max_length=255, help_text="Re-Type Email: ")
passwd = forms.CharField(widget=forms.PasswordInput(), max_length=100, help_text="Password: ")
rpasswd = forms.CharField(widget=forms.PasswordInput(), max_length=100, help_text="Re-Type Password: ")
class Meta:
model = User
fields = ()
def clean_email(self):
cd = self.cleaned_data
email = cd.get('email')
if validate_email(email):
raise forms.ValidationError("Please enter a proper email address")
def addUser(request):
context = RequestContext(request)
if request.method == 'POST':
form = register_form(request.POST)
password = request.POST['passwd']
username = request.POST['username']
email = request.POST['email']
objects = UserManager()
user = User.objects.create_user(username, email, password)
if form.is_valid():
user.set_password(password)
user.save()
else:
print form.errors
else:
form = register_form()
return render_to_response('testapp/register.html', {'register_form': register_form}, context)
<!DOCTYPE html>
<html>
<head>
<title>Registration</title>
</head>
<body>
<h1>Register with us</h1>
{% if registered %}
<a href="/rango/">Return to the homepage.</a><br />
{% else %}
<form id="register_form" method="post" action="/testapp/register/"
enctype="multipart/form-data">
{% csrf_token %}
{% for hidden in register_form.hidden_fields %}
{{ hidden }}
{% endfor %}
{% for field in register_form.visible_fields %}
{{ field.errors }}
{{ field.help_text }}
{{ field }}
<br>
{% endfor %}
<input type="submit" name="submit" value="Register" />
</form>
{% endif %}
</body>
</html>
You are using request.POST
before form validation that's not good. Change your code after form
validation
and use cleaned_data
for processing like
if request.method == 'POST':
form = register_form(request.POST)
if form.is_valid():
username = form.cleaned_data['username']
password = form.cleaned_data['passwd']
email = form.cleaned_data['email']
objects = UserManager()
user = User.objects.create_user(username, email, password)
user.set_password(password)
user.save()
else:
form = reqgister_form()
A good practice is to use the CBV
class UserCreateView(CreateView):
def get_form_class(self):
class _Form(forms.ModelForm):
passwd = forms.CharField(widget=forms.PasswordInput(), max_length=100, help_text="Password: ")
rpasswd = forms.CharField(widget=forms.PasswordInput(), max_length=100, help_text="Re-Type Password: ")
...
def clean(self):
...
class Meta:
fields = ('username', 'email', ...)
return _Form
def form_valid(self, form):
# additional logic
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.