Reputation: 22777
This is my view:
def registerView(request):
form = RegistrationForm()
if request.method == 'POST':
form = RegistrationForm(request.POST)
if form.is_valid():
user = User.objects.create_user(
username=form.cleaned_data['username'],
password=form.cleaned_data['password1'],
email=form.cleaned_data['email']
)
username=form.cleaned_data['username']
password=form.cleaned_data['password1']
user = authenticate(username=username, password=password)
login(request, user)
return redirect('/home/')
variables = {'registerForm':form}
return render(request, 'register.html', variables)
and this is my RegistrationForm:
class RegistrationForm(forms.Form):
username = forms.CharField(label='Username', max_length=30)
email = forms.EmailField(label='Email')
password1 = forms.CharField(label='Password', widget=forms.PasswordInput())
password2 = forms.CharField(label='Confirm Password', widget=forms.PasswordInput())
def clean_password2(self):
if 'password1' in self.cleaned_data:
password1 = self.cleaned_data['password1']
password2 = self.cleaned_data['password2']
if password1 == password2:
return password2
raise forms.ValidationError('Passwords do not match.')
def clean_username(self):
username = self.cleaned_data['username']
if not re.search(r'^\w+$', username):
raise forms.ValidationError('Username can only contain alphanumeric characters and the underscore.')
try:
User.objects.get(username=username)
except ObjectDoesNotExist:
return username
raise forms.ValidationError('Username is already taken.')
and this is my template:
<form method="post" action="/regsiter/">{% csrf_token %}
<h6>username:</h6>
{{ registerForm.username }}
<h6>email</h6>
{{ registerForm.email }}
<h6>password:</h6>
{{ registerForm.password1 }}
<h6>confirm password:</h6>
{{ registerForm.password2 }}
<input type="hidden" name="next" />
<input type="submit" value="register" />
</form>
Now for some reason, when I go to my template and not fill anything in (leave all fields blank) and try to register, it redirects to /home/ which means it is marking the form as valid (if form.is_valid(): returns true for some reason) even when all the fields are blank. I even tried using a username which exists but it still said that the form is valid. Any idea why?
Upvotes: 0
Views: 377
Reputation: 6790
The form action was spelt incorrectly and so it was never hitting the registerView
. It should be:
<form method="post" action="/register/">
Upvotes: 1