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 Clean a Field

Hamid Salehi
Hamid Salehi
5,721 Points

ValidationError problem, Can't get the right clean_honeypot function to work

what is wrong with this code? I can't figure it out

from django import forms

class LeadShareForm(forms.Form): email = forms.EmailField() link = forms.URLField()
honeypot = forms.CharField(required=False, widget=forms.HiddenInput, label="Leave Empty")

def clean_honeypot(self):
    honeypot = self.cleaned_data['honeypot']
    if len(honeypot):
        raise forms.ValidationError("Bad bot!")

    return honeypot
myproject/forms.py
from django import forms


class LeadShareForm(forms.Form):
    email = forms.EmailField()
    link = forms.URLField()   
    honeypot = forms.CharField(required=False, widget=forms.HiddenInput, label="Leave Empty")   

    def clean_honeypot(self):
        honeypot = self.cleaned_data['honeypot']
        if len(honeypot):
            raise forms.ValidationError("Bad bot!")

        return honeypot
Modou Sawo
Modou Sawo
13,141 Points

I'm having the same problem. The suggestion message still goes through

from django import forms


class SuggestionForm(forms.Form):
    name = forms.CharField()
    email = forms.EmailField()
    suggestion = forms.CharField(widget=forms.Textarea)
    honeypot = forms.CharField(required=False,
                                widget=forms.HiddenInput,
                                label="Leave empty")

    def clean_honeypot(self):
        honeypot = self.cleaned_data['honeypot']
        if len(honeypot):
            raise forms.ValidationError(
                "honeypot should be left empty. Bad bot!")
        return honeypot

1 Answer

Paul Blouin
Paul Blouin
6,502 Points

Same for me :) Cannot make it work...