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

ANGEL SERRANO PEREZ
ANGEL SERRANO PEREZ
9,855 Points

I dont know how to check capitalization and how to check if a part of a text is inside of another text

...

myproject/forms.py
from django import forms
from django.core import validators

def not_treehouse(value):
  if '@teamtreehouse.com' in value:
    raise forms.ValidationError('no tree')


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

1 Answer

Steven Parker
Steven Parker
215,984 Points

Remember, you're not just checking if it's inside, you want to check if it is at the end. And you don't necessarily have to check capitalization if you can somehow be sure the case is what you want.

Python strings have a number of handy built-in methods. One of them is .endswith(), which checks to see if the string ends with another string. And of course, you can always change a string to lower case with the .lower() method. And, you can chain methods together if you want to do more than one of them to the same string.

I'm betting with these hints you can figure it out.