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

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
229,732 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.