Reputation: 33
I have a strange problem with a form I have created for user registration using a Django customer user model. I'm running Django 1.6.5.
forms.py
class SignupForm(forms.ModelForm):
password1 = forms.CharField(widget=forms.PasswordInput,
label=_('Password'),
max_length=50)
password2 = forms.CharField(widget=forms.PasswordInput,
label=_('Repeat password'),
max_length=50)
class Meta:
model = RegisteredUser
fields = ("firstname", "lastname", "email", "password1", "password2",)
def clean_email(self):
existing = RegisteredUser.objects.filter(email__iexact=self.cleaned_data['email'])
if existing.exists():
raise forms.ValidationError(_('A user with that email already exists.'))
return self.cleaned_data['email']
def clean(self):
cleaned_data = super(SignupForm, self).clean()
if 'password1' in self.cleaned_data and 'password2' in self.cleaned_data:
if self.cleaned_data['password1'] != self.cleaned_data['password2']:
raise forms.ValidationError(_('The two passwords did not match.'))
return cleaned_data
views.py
class SignUpView(FormView):
template_name = 'core/signup.html'
form_class = SignupForm
success_url = None
def post(self, request, *args, **kwargs):
form = self.get_form(self.get_form_class())
if form.is_valid():
return self.form_valid(request, form)
else:
return self.form_invalid(form)
def form_valid(self, request, form):
new_user = self.register(request, **form.cleaned_data)
success_url = self.get_success_url(request, new_user)
try:
to, args, kwargs = success_url
return redirect(to, *args, **kwargs)
except ValueError:
return redirect(success_url)
def register(self, request, **cleaned_data):
RegisteredUser.objects.create_user(email=cleaned_data['username'],
firstname=cleaned_data['firstname'],
lastname=cleaned_data['lastname'],
password=cleaned_data['password2'])
The form returns invalid saying that 'password1' and 'password2' are required fields. The value is clearly present in the post data along with the other fields.
No matter what I have tried, 'password1' and 'password2' are not included in the cleaned_data dict. I have tried using various other names for these two fields, just to make sure 'password2' wasn't conflicting anything else.
I'm probably overlooking something extremely simple :-)
Thanks!
Mike
EDIT 1 - the template in use (sorry for the div-hell!):
<section class="container">
<div class="row">
<div class="col-md-6 col-sm-6">
<h2>Create an <strong>Account</strong></h2>
<form class="white-row" method="post" action="#">
{% csrf_token %}
<div class="row">
<div class="form-group">
<div class="col-md-12 col-sm-12">
<label>Firstname</label>
<input type="text" class="form-control" name="firstname">
</div>
</div>
</div>
<div class="row">
<div class="form-group">
<div class="col-md-12 col-sm-12">
<label>Lastname</label>
<input type="text" class="form-control" name="lastname">
</div>
</div>
</div>
<div class="row">
<div class="form-group">
<div class="col-md-12 col-sm-12">
<label>Email</label>
<input type="email" class="form-control" name="email">
</div>
</div>
</div>
<div class="row">
<div class="form-group">
<div class="col-md-6 col-sm-6">
<label>Password</label>
<input type="password" class="form-control" name="passsword1">
</div>
<div class="col-md-6 col-sm-6">
<label>Repeat password</label>
<input type="password" class="form-control" name="passsword2">
</div>
</div>
</div>
<div class="row">
<div class="col-md-12 col-sm-12">
<input type="submit" value="Sign up" class="btn btn-primary pull-right push-bottom" data-loading-text="Loading...">
</div>
</div>
</form>
</div>
Upvotes: 3
Views: 8615
Reputation: 10553
Here it is an example of how I do this in my projects:
forms.py:
class NewUserForm(forms.Form):
username = ...
password1 = forms.CharField(widget=forms.PasswordInput, required=True)
password2 = forms.CharField(widget=forms.PasswordInput, required=True)
def clean_password2(self):
password1 = self.cleaned_data.get("password1", "")
password2 = self.cleaned_data.get("password2", "")
if password1 and password2: # If both passwords has value
if password1 != password2:
raise forms.ValidationError(_(u"Passwords didn't match."))
else:
raise forms.ValidationError(_(u"Passwords can't be blank."))
return password2
views.py:
@login_required
def add_user(request):
''' Adds new user '''
if request.method == 'POST':
form = NewUserForm(request.POST)
if form.is_valid():
user = form.save()
# TODO: Save in temporary table
return HttpResponse('SUBMIT')
else:
form = NewUserForm()
return render_to_response('add_user.html', {'form': form}, context_instance=RequestContext(request))
template (form example):
<form action="." method="post" id="add_user_form">
{% csrf_token %}
{{ form.username }}
{{ form.password1 }}
{{ form.password2 }}
{{ form.usertype }}
<input type="submit" value="{% trans 'Save' %}" class="default"/>
</form>
{% csrf_token %}
: You need to put this in every form you use
action="."
: This make the post to the actual page
{{ form.VARIABLE_NAME}}
You need to set the inputs of the form like this, you create the form in the view, send to the template, and use it as a Django variable using {{ }}
. To set an input for one of your fields, like password1
it will be like I wrote above {{ form.password1}}
, you can also use {{form.errors}}
to check all form errors, or {{form.VARIABLE_NAME.errors}}
to check for exact field errors in the template
Upvotes: 1