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

My clean method is not passing, what could be the problem?

Django Forms: the clean method doesn't pass even though everything looks fine

myproject/forms.py
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):
        clean_data = super().clean()
        email = cleaned_data.get("email")
        email2 = cleaned_data.get("email2")
        if email != email2:
            raise forms.ValidationError("You need to enter the same email in both fields!")

1 Answer

Misspelling may possibly be the cause of this: clean_data instead of cleaned_data