Reputation: 23
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
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:
The form html can be included via:
Upvotes: 2
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
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