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

Oszkár Fehér
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Oszkár Fehér
Treehouse Project Reviewer

Some help here please

I understand the question like this. Please someone can explain what i am doing wrong?

myproject/forms.py
from django import forms
import re


def not_treehouse(value):
    if re.search(r'@[teamrhous]', value, re.I):
        raise forms.ValidationError("Bad robot")
    else:
        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,423 Points

The regular expression is a difficult road. For example, it might match "@meathousetree.moc" since the order of the letters in the regex character set do not care about order.

A more straightforward approach would be to use the builtin str method .endswith():

if value.lower().endswith("@teamtreehouse.com"):
Oszkár Fehér
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Oszkár Fehér
Treehouse Project Reviewer

This part i didn;t know, and it's first time i see this endswith() method. Like this it looks simple, i thought i have to use regular expression because of the email... I see that i am still far away to understand python and in the courses it's just a little mentioned. The second problem is that i have nobody to discuss about coding, i am alone in this, everything i learned here. I just hope everytime that someone will answer, i never had a direct discussion about python just your answers. I really like what i am learning here and i want to learn more because i hope one day i can change my profession with this and i am also aware that i still have to learn a lot. plus i am working very hard and i have time just when i have break and a few hours when i am home and i feel that this is not enough. Anyway, like usually, i am thankfull when someone takes his time to explain to me and not least thank you again! (and i am sorry that i wrote other things what is not connected to the question but felt the need to explain to someone this.)

Chris Freeman
Chris Freeman
Treehouse Moderator 68,423 Points

There are a lot of tricks you pick up the more you practice. A great way to gain skills is to look at other people's code to see their approach to a problem. Even after a years of programming I often rely on Google or StackOverflow for helpful hints. For example

https://www.google.com/search?q=python+check+if+string+ends+with

or

https://www.google.com/search?q=python+check+ends+with+regex

As for using a regex, in this case it can work, but as with most code, there usually is a simpler way. Using regex:

if re.search(r'@teamtreehouse.com$', value.lower(), re.I):