From ea2b27c887acab1d1eee724bf3403a2ff2cd6d96 Mon Sep 17 00:00:00 2001 From: Hugh Rundle Date: Sat, 23 Mar 2024 16:23:41 +1100 Subject: [PATCH] fix contact form fixes #3 fixes #4 --- blogs/forms.py | 12 ++++++++++++ blogs/templates/contact.html | 14 ++++---------- 2 files changed, 16 insertions(+), 10 deletions(-) diff --git a/blogs/forms.py b/blogs/forms.py index 3279350..3369745 100644 --- a/blogs/forms.py +++ b/blogs/forms.py @@ -1,6 +1,7 @@ """forms for use in views""" from django import forms +from django.core.exceptions import ValidationError from django.utils import timezone from django.utils.translation import gettext_lazy as _ @@ -137,6 +138,17 @@ class ContactForm(forms.Form): from_email = forms.EmailField(label="Email", max_length=200) subject = forms.CharField(label="Subject", max_length=200) message = forms.CharField(widget=forms.Textarea) + bot_check = forms.CharField( + label="What is usually stored in a library?", + max_length=10, + help_text="Checking that you are human" + ) + + def clean_bot_check(self): + """validate the bot check""" + data = self.cleaned_data["bot_check"] + if data != "books": + raise ValidationError("Try again. Think of something with pages.") class SubscribeViaMastodon(forms.Form): diff --git a/blogs/templates/contact.html b/blogs/templates/contact.html index 5c972ec..b9233ea 100644 --- a/blogs/templates/contact.html +++ b/blogs/templates/contact.html @@ -10,16 +10,10 @@
- - {% if errors %} {{ form.from_email.errors }} {% endif %} - {{form.from_email}} - - {% if errors %} {{ form.subject.errors }} {% endif %} - {{form.subject}} - {% if errors %} {{ form.message.errors }} {% endif %} - - - + {{form}} +
+ +