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

Cleaning Honeypot Field?

I'm trying to clean the honeypot field, but it fails when I check my work. What am I missing here?

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)
  def clean_honeypot(self):
    honeypot = self.cleaned_data['honeypot']
        if len(honeypot):
            raise forms.ValidationError('Error: Honeypot should be left empty.')
        return honeypot

1 Answer

Chris Freeman
MOD
Chris Freeman
Treehouse Moderator 68,427 Points

You code is close. There is an indention issue and the return should be in an else block:

class LeadShareForm(forms.Form):
  email = forms.EmailField()
  link = forms.URLField()
  honeypot = forms.CharField(required=False,widget=forms.HiddenInput)
  def clean_honeypot(self):
      honeypot = self.cleaned_data['honeypot']
      if len(honeypot):
          raise forms.ValidationError('Error: Honeypot should be left empty.')
      else:
          return honeypot

Why does it "HAVE" to be in else block?

Chris Freeman
Chris Freeman
Treehouse Moderator 68,427 Points

Deep Sukhwani, you are correct that the return does not have to be in an else block.