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 trialHenrik Christensen
Python Web Development Techdegree Student 38,322 PointsProblems with my FormView
Hi,
I'm trying to make a contact form using a FormView.
My problem is that if the honeypot field is being filled out, then I don't get the error-msg from the forms.ValidationError().
I've read on stackoverflow that it's because the FormView is redirecting? and don't bring the error-msg with it?
Any ideas how to fix this?
views.py
from django.contrib import messages
from django.core.mail import send_mail
from django.urls import reverse_lazy
from django.views import generic
from . import forms
class ContactView(generic.FormView):
form_class = forms.ContactForm
template_name = 'contacts/contact.html'
success_url = reverse_lazy('contact:contact_me')
def form_valid(self, form):
send_mail(
'Contact from henrikac.com',
'From {name} <{email}>\n\n{message}'.format(**form.cleaned_data),
'from@me.com',
['to@me.com']
)
messages.success(
self.request,
'Thank you very much for your message. I will get back to you as soon as possible'
)
return super().form_valid(form)
forms.py This is identical to the one Kenneth Love made in one of his forms videos
from django import forms
class ContactForm(forms.Form):
name = forms.CharField()
email = forms.EmailField(label='Email address')
message = forms.CharField(widget=forms.Textarea(attrs={'rows': 5}))
honeypot = forms.CharField(required=False,
widget=forms.HiddenInput,
label='Leave empty')
def clean_honeypot(self):
honeypot = self.cleaned_data['honeypot']
if len(honeypot) > 0:
raise forms.ValidationError(
'Honeypot should be left empty. Bad bot!'
)
return honeypot
1 Answer
Henrik Christensen
Python Web Development Techdegree Student 38,322 PointsTurns out that by cleaning a specific field attribute (clean_honeypot) is turning errors into field.errors instead of non_fields_errors - and to fix this I had to do use the clean() method instead.
def clean(self):
cleaned_data = super().clean()
honeypot = self.cleaned_data['honeypot']
if len(honeypot) > 0:
raise forms.ValidationError(
'Honeypot should be left empty. Bad bot!'
)
return cleaned_data
Still thinking that there has to be a better solution? But this is working for now :-)
Chris Freeman
Treehouse Moderator 68,441 PointsChris Freeman
Treehouse Moderator 68,441 PointsNice! Not sure there is a better way.
Henrik Christensen
Python Web Development Techdegree Student 38,322 PointsHenrik Christensen
Python Web Development Techdegree Student 38,322 PointsNot sure why the error msg display correctly when Kenneth Love use the clean_honeypot and not when I do - doing exactly like he did :-P
Chris Freeman
Treehouse Moderator 68,441 PointsChris Freeman
Treehouse Moderator 68,441 PointsWhich version of Django are you using? How does it compare to the version found in the workspace paired with the video?
Henrik Christensen
Python Web Development Techdegree Student 38,322 PointsHenrik Christensen
Python Web Development Techdegree Student 38,322 PointsI'm using django 2.0 and he is using 1.8 in the video
Chris Freeman
Treehouse Moderator 68,441 PointsChris Freeman
Treehouse Moderator 68,441 PointsI'm still ramping up on 2.0, it wouldn't surprise me if that's what is causing the difference in behavior.
Henrik Christensen
Python Web Development Techdegree Student 38,322 PointsHenrik Christensen
Python Web Development Techdegree Student 38,322 PointsI guess you're right :-)