pinplex
pinplex

Reputation: 23

Django-forms on every Wagtail page, e.g. Contact form in the footer of base.html

I want to implement a contact form using django-forms into the footer of a multi-page website based on the wagtail cms framework. How can I render the form in the base.html template on every page? Thank you!

Upvotes: 0

Views: 863

Answers (3)

allcaps
allcaps

Reputation: 11248

Although you want the contact form to appear on every page, I would still make a dedicated ContactFormPage AND place the form in every footer. The POST request should point to this dedicated ContactFormPage.

<form action='{% pageurl contact_page' %}' ...>

The advantages are:

  • when the contact form contains errors, you are on the dedicated page with the focus on the task at hand.
  • No need to scroll back to the footer when an error occurs.
  • Easy to add a success page via RoutablePageMixin
  • A sharable contact form url

The form html can be included via:

  • the inclusion template tag (Gasmans answer)
  • middleware (shouravs answer)

Upvotes: 2

gasman
gasman

Reputation: 25292

I'd suggest implementing it as an inclusion template tag:

@register.inclusion_tag('contact_form.html')
def contact_form():
    return {'form': ContactForm()}

The contact_form.html template would contain the form's HTML. You can then include this as part of your base.html with the tag: {% contact_form %}

Upvotes: 2

shourav
shourav

Reputation: 996

create a middleware.py in the root folder where settings.py resides. Then add this in that file

class SimpleMiddleware:
    def __init__(self, get_response):
        self.get_response = get_response
        # One-time configuration and initialization.

    def __call__(self, request):
        # Code to be executed for each request before
        # the view (and later middleware) are called.
        request.contact_form = ContactForm()
        response = self.get_response(request)

        # Code to be executed for each request/response after
        # the view is called.

        return response

Upvotes: 0

Related Questions