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 trialNoam Sellam
3,923 Pointscan't pass the custom validators challenge in django forms course -
how do i validate the email correctly?
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
Treehouse Moderator 68,441 PointsYou 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
6,727 PointsHasan Ahmad
6,727 PointsWhy do we have to return Value??
Chris Freeman
Treehouse Moderator 68,441 PointsChris Freeman
Treehouse Moderator 68,441 PointsHasan 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 incleaned_data['honeypot']
. Even though the methodclean_honey_pot
doesn't actually modify the data, it still needs to pass it back to the data cleaning code.