Python Forum

Full Version: How to Prevent Double Submission in Django
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I would like to prevent double submission in Django. The problem arises when the user clicks on the back button in the browser. I know that I can use JavaScript, but I would like to fix the issue using Python.

I also attempted to resolve the issue by using HttpResponseRedirect, but it did not resolve the problem. Any suggestions would be greatly appreciated. Thank you!

Here is the code:

views.py
class HandleChannelFormView(FormView):
    template_name = 'details.html'
    template_added = "success.html"
    form_class = MediaForm
    success_url = reverse_lazy('data')

    def form_valid(self, form):
        data = self.request.session.get('data', None)

        if not data:
            messages.error(self.request, 'No channel data found in session.')
        else:
            category_id = form.cleaned_data['category'].id if form.cleaned_data['category'] else None
            language_id = form.cleaned_data['language'].id if form.cleaned_data['language'] else None

            data.update({
                'body': form.cleaned_data['body'],
                'category_id': category_id,
                'category_name': form.cleaned_data['category'].name if form.cleaned_data['category'] else None,
                'language_id': language_id,
                'nsfw': form.cleaned_data['nsfw'],  # Add the nsfw value to the data dictionary
            })
            counter = self.request.session.get('counter', {})
            StoreChannelData.save_data(data, counter)

        return render(self.request, self.template_added, {'data': data, 'media_form': form})
You can implement the Post/Redirect/Get (PRG) pattern and use Django's built-in features to protect against duplicate form submissions.
Here's how you can modify your HandleChannelFormView to handle this:-


1.> Use Django's built-in messages framework to display success messages.
2.> Implement the PRG pattern by redirecting after a successful form submission.


from django.contrib import messages
from django.shortcuts import redirect
from django.urls import reverse_lazy
from django.views.generic.edit import FormView
from .forms import MediaForm
from .models import StoreChannelData

class HandleChannelFormView(FormView):
    template_name = 'details.html'
    template_added = "success.html"
    form_class = MediaForm
    success_url = reverse_lazy('data')
 
    def form_valid(self, form):
        data = self.request.session.get('data', None)
 
        if not data:
            messages.error(self.request, 'No channel data found in session.')
        else:
            category_id = form.cleaned_data['category'].id if form.cleaned_data['category'] else None
            language_id = form.cleaned_data['language'].id if form.cleaned_data['language'] else None
 
            data.update({
                'body': form.cleaned_data['body'],
                'category_id': category_id,
                'category_name': form.cleaned_data['category'].name if form.cleaned_data['category'] else None,
                'language_id': language_id,
                'nsfw': form.cleaned_data['nsfw'],
            })
            counter = self.request.session.get('counter', {})
            StoreChannelData.save_data(data, counter)
 
            # Redirect after successful form submission to prevent double submission
            return redirect(self.success_url)

        return render(self.request, self.template_added, {'data': data, 'media_form': form})
With this update, after the form is successfully submitted, the view will perform a redirect to the specified success_url using redirect(). The user will be taken to the success page using a "GET" request, avoiding the possibility of resubmitting the form when the user clicks the "back" button in the browser.