Nate Reed
Nate Reed

Reputation: 7011

Django form.is_valid() not calling method clean_<fieldname>

I copied a simple example of a user registration page in Django 1.0 Web Site Development. I have defined the following form:

class RegistrationForm(forms.Form):
    username = forms.CharField(label=u'Username', max_length=30)
    email = forms.EmailField(label=u'Email')
    password1 = forms.CharField(
        label=u'Password',
        widget=forms.PasswordInput()
    )
    password2 = forms.CharField(
        label=u'Password (Again)',
        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):
        print "Validating username..."
        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 User.DoesNotExist:
            return username
        raise forms.ValidationError('Username is already taken')

In the case where a username is already taken, the field is validated using the normal form validation and my custom clean method is not called. When the form is submitted to register_page, form.is_valid() returns True.

Is there something missing that needs to be done in order for Django to know to call the clean_ methods?

def register_page(request):
    if request.method == 'POST':
        print "Posted to registration form"
        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']
            )
            return HttpResponseRedirect('/')
    else:
        form = RegistrationForm()

    return render_to_response(
        'users/registration.html',
        context_instance=RequestContext(request, {
                'form' : form
                })

Upvotes: 3

Views: 4169

Answers (2)

Rob Osborne
Rob Osborne

Reputation: 4997

In case anybody else gets here via google and the above answer doesn't help:

I had this same problem and it turned out to be I was not setting exclude as a tuple in my forms meta:

class MyFormClass(ModelForm):
    class Meta:
        exclude = ('type_detail')  #wrong

instead of:

class MyFormClass(ModelForm):
    class Meta:
        exclude = ('type_detail', )  #right

Upvotes: 4

Nate Reed
Nate Reed

Reputation: 7011

This was truly puzzling. I restored a auto-save version of forms.py and it magically started working, yet there is no difference that I can discern between the code snippet above, and the restored file.

Short answer: "Programmer Error"

Upvotes: 0

Related Questions