2021-02-21 12:07:10 +00:00
# How to use a redirect with Form builder to prevent double submission
It is common for form submission HTTP responses to be a `302 Found` temporary redirection to a new page.
By default `wagtail.contrib.forms.models.FormPage` success responses don't do this, meaning there is a risk that users will refresh the success page and re-submit their information.
Instead of rendering the `render_landing_page` content in the POST response, we will redirect to a `route` of the `FormPage` instance at a child URL path.
The content will still be managed within the same form page's admin.
2022-04-15 12:12:33 +00:00
This approach uses the additional contrib module `wagtail.contrib.routable_page` .
2021-02-21 12:07:10 +00:00
An alternative approach is to redirect to an entirely different page, which does not require the `routable_page` module.
2022-04-05 10:29:34 +00:00
See [](form_builder_custom_landing_page_redirect).
2021-02-21 12:07:10 +00:00
2022-04-05 10:29:34 +00:00
Make sure `"wagtail.contrib.routable_page"` is added to `INSTALLED_APPS` , see [](routable_page_mixin) documentation.
2021-02-21 12:07:10 +00:00
```python
from django.shortcuts import redirect
from wagtail.contrib.forms.models import AbstractEmailForm
2021-05-04 06:43:04 +00:00
from wagtail.contrib.routable_page.models import RoutablePageMixin, path
2021-02-21 12:07:10 +00:00
class FormPage(RoutablePageMixin, AbstractEmailForm):
# fields, content_panels, …
2021-05-04 06:43:04 +00:00
@path ("")
2021-02-21 12:07:10 +00:00
def index_route(self, request, *args, * *kwargs):
"""Serve the form, and validate it on POST"""
return super(AbstractEmailForm, self).serve(request, *args, * *kwargs)
def render_landing_page(self, request, form_submission, *args, * *kwargs):
"""Redirect instead to self.thank_you route"""
url = self.reverse_subpage("thank_you")
# If a form_submission instance is available, append the ID to URL.
if form_submission:
url += "?id=%s" % form_submission.id
return redirect(self.url + url, permanent=False)
2021-05-04 06:43:04 +00:00
@path ("thank-you/")
2021-02-21 12:07:10 +00:00
def thank_you(self, request):
"""Return the superclass's landing page, after redirect."""
form_submission = None
try:
submission_id = int(request.GET["id"])
except (KeyError, TypeError):
pass
else:
submission_class = self.get_submission_class()
try:
form_submission = submission_class.objects.get(id=submission_id)
except submission_class.DoesNotExist:
pass
return super().render_landing_page(request, form_submission)
```