Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Python Django Forms Forms Clean Two Fields

Lewis Cowles
Lewis Cowles
74,902 Points

:'( My form clean won't work

so I have no clue why this is not working... looks to be the same to me as the example in the video. Kenneth Love ideas?

from django import forms


def not_treehouse(value):
    if value.lower().endswith('@teamtreehouse.com'):
        raise forms.ValidationError('Just come talk to me')


class LeadShareForm(forms.Form):
    email = forms.EmailField(validators=[not_treehouse])
    email2 = forms.EmailField(label='Email again', validators=[not_treehouse])
    link = forms.URLField()
    honeypot = forms.CharField(widget=forms.HiddenInput, required=False)

    def clean_honeypot(self):
        honey = self.cleaned_data['honeypot']
        if len(honey):
            raise forms.ValidationError('Bad robot!')
        return honey

    def clean(self):
        cleaned = super.clean();
        email = cleaned.get('email')
        verify = cleaned.get('email2')
        if email != verify:
            raise forms.ValidationError('Hey!')

1 Answer

Chris Freeman
MOD
Chris Freeman
Treehouse Moderator 68,457 Points

I see two differences: super() needs parens, and a trailing semi-colon:

    def clean(self):
        cleaned = super().clean()  # <-- add parens, remove semi-colon
        email = cleaned.get('email')
        verify = cleaned.get('email2')
        if email != verify:
            raise forms.ValidationError('Hey!')
Lewis Cowles
Lewis Cowles
74,902 Points

I must be blind, thx Chris