Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Python Django Forms Forms Create a Validator

Validation Function for not_treehouse does not pass challenge task 1 of 2 (Series: Django Forms)

For some reason I can't seem to get this to pass. I have tried a few methods but so far I can't seem to get this to pass. Only thing I can think of is that it doesn't check for if someone is passing in a None value but the error that I am getting doesn't indicate that.

The error says that I need to make sure that I take capitalization in to consideration which I am pretty sure I have. Testing this in ipython it agrees with me.

In [5]: not_treehouse('test@example.com')                                                                                                                                                                                                                                                                                                                                                   
Out[5]: True

In [6]: not_treehouse('test@teamtreehouse.com')                                                                                                                                                                                                                                                                                                                                             
Out[6]: False

In [7]: not_treehouse('test@teamtreehouSe.com')                                                                                                                                                                                                                                                                                                                                             
Out[7]: False

In [8]: not_treehouse('test@TeamTreehouSe.com')                                                                                                                                                                                                                                                                                                                                             
Out[8]: False

In [9]: not_treehouse('test@TeamTreehouSes.com')                                                                                                                                                                                                                                                                                                                                            
Out[9]: True

In [10]: 'test@example.com'.endswith('@example.com')                                                                                                                                                                                                                                                                                                                                        
Out[10]: True

In [11]: 'test@example.com'.endswith('@teamtreehouse.com')                                                                                                                                                                                                                                                                                                                                  
Out[11]: False

In [12]: email = 'Test.Person@ExAmpLe.com'                                                                                                                                                                                                                                                                                                                                                  

In [13]: email.lower().endswith('@example.com')                                                                                                                                                                                                                                                                                                                                             
Out[13]: True

I appreciate any advice that I can get on this.

myproject/forms.py
from django import forms


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

def not_treehouse(email):
    if email.lower().endswith('@teamtreehouse.com'):
        raise ValidationError('Must not use a teamtreehouse.com email address!')

The exact error that I am getting is

Bummer: Make sure the validator does not allow @teamtreehouse.com email addresses, regardless of capitalization.; Make sure the validator does not allow @teamtreehouse.com email addresses.

1 Answer

oh geez, so I completely forgot that in order to use the ValidationError I have to use that method from the forms module. So to fix this I needed to use forms.ValidationError

def not_treehouse(email):
    if email.lower().endswith('@teamtreehouse.com'):
        raise forms.ValidationError('Must not use a teamtreehouse.com email address!')