For Wagtail 5.x and 6.x
A very basic contact form with CAPTCHA
module that protects you against spam based on two articles from LearnWagtail website ("Contact Forms" and "Adding Recaptcha to Your Contact Forms").
Note
reCAPTCHA V3
is now supported, while reCAPTCHA V2
is deprecated.
-
Install the package from
GitHub
.pip install git+https://github.com/FilipWozniak/wagtail-contact-form.git
-
Add the application and its dependencies to the
INSTALLED_APPS
in your settings file.INSTALLED_APPS = [ "contact_form", ### dependencies: "django_recaptcha", "widget_tweaks" ]
Most systems will already have
wagtail.contrib.forms
in INSTALLED_APPS - if not, add it, too.
If you want install a Python
application in editable mode, you can use the editable mode provided by pip
.
-
Clone the application's source code:
git clone https://github.com/FilipWozniak/wagtail-contact-form .
-
Navigate to the root directory of the application's source code in the terminal or command prompt.
-
Install the application in editable mode.
Use the pip install command with the
-e
or--editable
flag followed by a period (.
) to specify the current directory (where the application'ssetup.py
file is located).pip install -e .
Replace the
.
with the path to the directory if you're running the command from a different location. -
Add the application and its dependencies to the
INSTALLED_APPS
in thesettings.py
file.INSTALLED_APPS = [ "contact_form", ### dependencies: "django_recaptcha", "widget_tweaks" ]
Most systems will already have
wagtail.contrib.forms
in INSTALLED_APPS - if not, add it, too.
-
Register
reCAPTCHA V3
keys in the reCAPTCHA Admin console. -
Add the following entries to the
settings.py
file.RECAPTCHA_PUBLIC_KEY = '' RECAPTCHA_PRIVATE_KEY = '' RECAPTCHA_REQUIRED_SCORE = 0.85 RECAPTCHA_DOMAIN = 'www.recaptcha.net'
-
Configure your email settings (
EMAIL_BACKEND
,EMAIL_HOST
etc.), as without these settingsDjango
will most likely returnConnectionRefusedError
while attempting to submit the form. -
Create a page of new type: "Contact Page". Add required fields to the contact form (see section "Form Fields" below.)
Note that if you are using MacOS you may stumble across URLError
while trying to submit the form.
URLError at /contact-us/
<urlopen error [SSL: CERTIFICATE_VERIFY_FAILED] Certificate Verify Failed: Unable to Get Local Issuer Certificate (_ssl.c:1122)>
As regards to "Scraping: SSL: CERTIFICATE_VERIFY_FAILED" issue on Stack Overflow, all you need to do is go to Macintosh HD → Applications → Python folder and double click on Install Certificates.command file.
Full Name
E-Mail Address
Message
CAPTCHA
[!WARNING] As you can see from the code snippet below, form fields are not rendered dynamically, which means you need to name labels identically as mentioned above —
Full Name
,E-Mail Address
,Message
(theCAPTCHA
field is generated automatically, you do not need to define it in the backend).
<div class="col-12 col-sm-6">
<div class="form-group mb-3">
<label for="full_name">Full Name</label>
{% render_field form.full_name placeholder=form.full_name.label class="form-control" %}
<small class="form-text text-muted">{{ form.full_name.field.help_text }}</small>
</div>
</div>
[!NOTE] Please remember that if you have saved a form with different labels than those mentioned, you must delete the form page and create it again with the correct values.
Intro Text
Thank You Text
E-Mail ("From" Address)
E-Mail ("To" Address)
E-Mail Subject
cd "project"
pytest -s wagtail_contact_form/contact_form/tests/unit --disable-pytest-warnings
cd "project"
(cd "wagtail_contact_form/testproject" && DJANGO_SETTINGS_MODULE=testproject.settings.base pytest -s ../contact_form/tests/unit --disable-pytest-warnings)
cd "project"
(cd "wagtail_contact_form" && pre-commit run --files contact_form/tests/unit/*)