Skip to main content

The Wagtail Form Builder functionalities available for your models/snippets.

Project description

Wagtail Model Forms

Version CI

The Wagtail Form Builder functionalities available for your models/snippets.

Features

  • Well-known Form Builder functionalities
  • Form block for StreamField support
  • Extensible models (Form, FormSubmission, UploadedFile & FormBlock)
  • Inspect, edit and delete views for form submissions
  • File uploads
  • Reports
  • Email notifications
  • Webhooks & API integrations
  • Crispy forms support

Requirements

  • Python >= 3.8
  • Django >= 4.2
  • Wagtail >= 5.2

Installation

Install the package

pip install wagtail-model-forms

Add wagtail_model_forms to your INSTALLED_APPS

INSTALLED_APPS = [
    ...
    "wagtail_model_forms",
]

Create your models

from django.utils.functional import cached_property
from modelcluster.fields import ParentalKey
from wagtail.core.models import Page
from wagtail.snippets.models import register_snippet
from wagtail_model_forms.mixins import FormSnippetMixin
from wagtail_model_forms.models import AbstractForm, AbstractUploadedFile, AbstractFormSubmission


class FormSubmission(AbstractFormSubmission):
    form = models.ForeignKey(
        "Form",
        on_delete=models.CASCADE,
        related_name="+",
        verbose_name=_("Form"),
    )


class UploadedFile(AbstractUploadedFile):
    pass


@register_snippet
class Form(AbstractForm):
    @cached_property
    def edit_url(self):
        # return your url here, commonly your own modeladmin configuration
        url_helper = FormModelAdmin().url_helper
        return url_helper.get_action_url("edit", self.id)


class MyPage(FormSnippetMixin, Page):
    pass

In order to let the page now that there is a form on it (handle post methods, caching etc.) we need to make sure the method page_has_form returns True. To make your life easier you can set the property streamfields with the names of the StreamField which can have the form added. Also make sure the name of the block is matched, commonly called form in your list of blocks. In some cases you might want to override this method and implement your own bespoke logic here.

from wagtail_model_forms.blocks import FormBlock


class MyPage(FormSnippetMixin, Page):
    block_type = "form" # has by default already the value form
    streamfields = ["content"] # the name of your streamfields
    
    content = StreamField([
      # your other blocks
      ("form", FormBlock()),
    ])

More advanced with your bespoke implementation

from django.utils.functional import cached_property


class MyPage(FormSnippetMixin, Page):
    @cached_property
    def page_has_form(self):
        # your bespoke import in order to determine a form is on the page

It's not mandatory to use the register_snippet functionality. You can e.g. use wagtailmodelchooser for it or any other bespoke implementation in order to put the form on your page.

Settings

WAGTAIL_MODEL_FORMS_ADD_NEVER_CACHE_HEADERS`

Default True

WAGTAIL_MODEL_FORMS_FORM_MODEL

Must be of the form app_label.model_name

WAGTAIL_MODEL_FORMS_SUBMISSION_MODEL

Must be of the form app_label.model_name

WAGTAIL_MODEL_FORMS_UPLOADED_FILE_MODEL

Must be of the form app_label.model_name

WAGTAIL_MODEL_FORMS_REPORTS`

Default True

WAGTAIL_MODEL_FORMS_CIRSPY_FORMS_FORM_TAG

Default False

Templates

wagtail_model_forms/form.html

{% if not request.form_success is self.form.id %}
<form action="" method="POST" novalidate>
    {% csrf_token %}
    <input type="hidden" name="form_id" value="{{ self.form.id }}">
    {{ form.as_ul }}
    <button type="submit">Submit</button>
</form>
{% else %}
    <p>Form success</p>
{% endif %}

FAQ

The form is not submitted

Make sure you have added the FormSnippetMixin and implemented the page_has_form method correctly.

Request is not available in get_context of FormBlock

Make sure you render your blocks in your templates with include_block, see https://docs.wagtail.org/en/stable/topics/streamfield.html.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

wagtail_model_forms-0.9.0.tar.gz (16.0 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

wagtail_model_forms-0.9.0-py2.py3-none-any.whl (21.1 kB view details)

Uploaded Python 2Python 3

File details

Details for the file wagtail_model_forms-0.9.0.tar.gz.

File metadata

  • Download URL: wagtail_model_forms-0.9.0.tar.gz
  • Upload date:
  • Size: 16.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.6

File hashes

Hashes for wagtail_model_forms-0.9.0.tar.gz
Algorithm Hash digest
SHA256 9bdbb054ef2934c8d4c3fdd2eb712c446432a6ac135cf371288a7545955a3f1b
MD5 0690f8a7527f94e45adbc05dc2a25fb7
BLAKE2b-256 4f7d6a970982303444895c3eb19d707cc3e0a13b0cf9b8bae6b60fc3e80632ae

See more details on using hashes here.

File details

Details for the file wagtail_model_forms-0.9.0-py2.py3-none-any.whl.

File metadata

File hashes

Hashes for wagtail_model_forms-0.9.0-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 790f742d61387bf860e65bfde4ecf99595443d14186e86315a82f2200a0aef7e
MD5 6da90fd9efc009abfce2d61eaa0e3a46
BLAKE2b-256 aafd60b9602390c10d7c5a0d8030965a2528267649fd757882f42f679f127938

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page