New Wagtail Course! 🥳 The Ultimate Wagtail Developers Course

Tutorial Wagtail Version: 2.x

Contact Forms

Contact forms are an essential part of many websites. Wagtail gives us a lot of powerful features without having to do very much work. In this tutorial we're going to create a contact form and a contact form landing page. The contact form will have 3 completely custom fields that the content editor can manage on their own. We'll also explore the Forms feature in the Wagtail Admin.

Most websites have some type of contact form. Whether it's for support or some other reason, there's usually a way to get in touch with a websites maintainer.

Contact forms can be a hassle though. But in this tutorial I'll show you how to add an email form for your Wagtail site. It's super easy, and there are lots of goodies out of the box.

Settings

First things first, you'll need to make sure you have wagtail.contrib.forms in your INSTALLED_APPS

``` INSTALLED_APPS = [ '...', 'wagtail.contrib.forms', '..', ] ```
Models

Next you'll need to create two classes. One will handle the email fields, and one will be your actual Wagtail Page.

``` # Your models.py file from django.db import models from modelcluster.fields import ParentalKey from wagtail.admin.edit_handlers import ( FieldPanel, FieldRowPanel, InlinePanel, MultiFieldPanel ) from wagtail.core.fields import RichTextField from wagtail.contrib.forms.models import ( AbstractEmailForm, AbstractFormField ) class FormField(AbstractFormField): page = ParentalKey( 'ContactPage', on_delete=models.CASCADE, related_name='form_fields', ) class ContactPage(AbstractEmailForm): template = "contact/contact_page.html" # This is the default path. # If ignored, Wagtail adds _landing.html to your template name landing_page_template = "contact/contact_page_landing.html" intro = RichTextField(blank=True) thank_you_text = RichTextField(blank=True) content_panels = AbstractEmailForm.content_panels + [ FieldPanel('intro'), InlinePanel('form_fields', label='Form Fields'), FieldPanel('thank_you_text'), MultiFieldPanel([ FieldRowPanel([ FieldPanel('from_address', classname="col6"), FieldPanel('to_address', classname="col6"), ]), FieldPanel("subject"), ], heading="Email Settings"), ] ```

When you're done with that, make sure you run python manage.py makemigrations and python manage.py migrate

Templates

There are two templates you're going to work with.

  1. Your main contact page template, and
  2. The landing page template (landing page is the term used for "the form was submitted, show a different page")
``` # contact_page.html {% extends 'base.html' %} {% load wagtailcore_tags %} {% block content %} <h1>{{ page.title }}</h1> <p> {{ self.intro|richtext }} </p> <form action="{% pageurl page %}" method="POST"> {% csrf_token %} <ul> {{ form.as_ul }} </ul> <button type="submit"> Submit Form </button> </form> {% endblock %} ```
``` # contact_page_landing.html {% extends 'base.html' %} {% load wagtailcore_tags %} {% block content %} <h1>{{ page.title }}</h1> <p> {{ self.thank_you_text|richtext }} </p> {% endblock %} ```

Lastly you'll need to create a new Contact Page from inside the Wagtail Admin. Add the fields you'd like to display. And that's it!

The Git Commit

The full git commit can be found here: https://github.com/CodingForEverybody/learn-wagtail/commit/d67bc5659ca023c21d8a9f01d30604f6d67cd2d6. Note: the git commit has Bootstrap 4 styling in it, and the code above (in this tutorial) has all the BS4 styling stripped out.

Related tutorials

Adding Recaptcha to Your Contact Forms

Posted on

Contact forms are notorious for collecting spam from bots. In this tutorial we'll install a package to automatically add Google Recaptcha to our contact forms. We'll explore how to get the API keys, as well.

View lesson, Adding Recaptcha to Your Contact Forms

Registering Snippets (Blog Category) using Checkboxes

Posted on

In this tutorial we'll be learning how to register another Wagtail Snippet, but instead of using an Orderable and a SnippetChooserPanel (like the previous lesson) we're going to use a ParentalManyToManyField and a form widget to create Checkboxes in the Wagtail Admin.

View lesson, Registering Snippets (Blog Category) using Checkboxes

Restricting Parent and Child Pages

Posted on

When you start to create several different page types in Wagtail and you create a child page (a page that's nested under a different page) you'll see every single page is an option. In this video we'll explore how to restrict where pages can live by telling the parent page what types of child page(s) they should expect, and by telling the child pages which parent page(s) they can expect.

View lesson, Restricting Parent and Child Pages

How to Subclass Wagtail Pages

Posted on

Subclassing is having a class (in this case it's a Wagtail Page) that can be used for other classes (Wagtail Page's). The parent class has all the common attributes for the child pages, and every child page will inherit everything from it's parent. In this lesson we'll explore that by creating a subclassed Article and Video Blog Page that share a common parent, and then we'll extend the functionality of both subclassed pages by adding new fields.

View lesson, How to Subclass Wagtail Pages

The Ultimate Wagtail Developers Course

This course covers everything from basic installation to advanced features like custom blocks and API integration, it's perfect for developers looking to enhance their skills with this powerful CMS.

Ultimate Wagtail Developers Course Logo