2

How to handle second submission duplicate request If user was trying to refresh the page when the first submission is not finished yet because of server lag.

  • client side disabling submit button to avoid multiple submits.
  • and handled Post/Redirect/Get pattern after form submit redirect to success view

I believe both are handled well.

class SomeView(View):
  def post(self, request, *args, **kwargs):
    if form.is_valid()
      if request_exists(request):
        # here I can raise the exception also 
        # But How I redirect the customer to sucess page 
        # If 1st submission got success response.
      else:
        # here I have called internal api to get/post some data.
        # user refreshes before this call has completed.
        ...
        # once getting respose its ALWAYS redirect to new page
        return HttpResponseRedirect('/thanks/') 

But how to handle the case If delay from getting a response from API call. I need to delay until the first submission finishes. Then I have to send the user to the thanks page.

1
  • 1
    The solution described here. If we have lengthy API call or any job in your view simply trigger it in a separate offline task and redirecting the user to new page ASAP with some waiting message. Commented Nov 1, 2017 at 8:51

1 Answer 1

0

It's possible via python, but it will be very complex. There is a way easier way, to accomplish your goal. Just user JQuery and disable the button, once the user clicks on it. That way the user cannot click it twice.

In the template, where you have your form for the view, add the following script (adjust to your needs):

$(document).ready(function(){
    var myButton = $('#my_button_id');  // your button ID here
    myButton.on('click', function(){
        myButton.prop('disabled', true);
    });
};

With JQuery, you can also chagne the button name to a spinner, so it looks like it is loading. I user FontAwesome for that (http://fontawesome.io/icon/spinner/).

Sign up to request clarification or add additional context in comments.

2 Comments

Yes, this will work But here the question is How to prevent duplicate submission came from browser refreshes. Disabling submit button from JS, Post/Reedirect/Get both handled in view
A note about fontawesome usage: it's a heavy file, don't import it only to get a spinner on the webpage.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.