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 Create a Validator

can't pass the custom validators challenge in django forms course -

how do i validate the email correctly?

myproject/forms.py
from django import forms

def not_treehouse(value):
        if "@teamtreehouse.com".lower() not in value.lower():
            raise forms.ValidationError('hey!!')
        return value

class LeadShareForm(forms.Form):
    email = forms.EmailField()
    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

1 Answer

Chris Freeman
MOD
Chris Freeman
Treehouse Moderator 68,441 Points

You have the right idea, the issue is your polarity is reversed: the error should be raised if "@teamtreehouse.com" is in the email address. So, remove the "not" from the if statement.

Hasan Ahmad
Hasan Ahmad
6,727 Points

Why do we have to return Value??

Chris Freeman
Chris Freeman
Treehouse Moderator 68,441 Points

Hasan Ahmad, good question! The clean functions in Django, if defined, are used to provide additional form data cleaning and inspection beyond the standard built-in form data sanitation performed in cleaned_data. If a clean method is defined, it is automatically called by the form data cleaning code which expects to receive back data (honey in this case) to replace the data that was already in cleaned_data['honeypot']. Even though the method clean_honey_pot doesn't actually modify the data, it still needs to pass it back to the data cleaning code.