Python Forum
How to Prevent Double Submission in Django
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to Prevent Double Submission in Django
#1
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})
Reply
#2
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.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Automating Captcha form submission with Mechanize Dexty 2 3,339 Aug-03-2021, 01:02 PM
Last Post: Dexty
  prevent getting blocked maneesh7787 3 2,339 Dec-11-2019, 08:41 AM
Last Post: buran
  Django: How to automatically substitute a variable in the admin page at Django 1.11? m0ntecr1st0 3 3,350 Jun-30-2019, 12:21 AM
Last Post: scidam

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020