Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Double clicking submit button creates duplicate records #2511

Open
elichad opened this issue Aug 9, 2023 · 3 comments
Open

Double clicking submit button creates duplicate records #2511

elichad opened this issue Aug 9, 2023 · 3 comments

Comments

@elichad
Copy link
Contributor

elichad commented Aug 9, 2023

In views that create a new record (i.e. not those that update an existing one), double clicking on the submit button can reliably create a duplicate record - i.e. the POST request is created and handled twice.

I keep running into this because of my particular (unusual) mousing patterns and impatience with slightly slow page loads. I've replicated this locally and in test-amy, but haven't tried production. It's slightly annoying, but generally harmless.

I assume that there are some common strategies to avoid this for websites in general, and that we can look into applying one of those here.

Screenshot of fake trainee with duplicate Welcome Session and Training records

@maneesha
Copy link
Contributor

From @elichad - Unsure about what it would take to fix it. Has seen it handled on other sites; need to research how this happens. Possibly disable button after clicking once to prevent it from being clicked again.

@maneesha maneesha moved this from To do - Unprioritized to To do - High priority in Miscellaneous issue backlog Aug 15, 2023
@elichad elichad self-assigned this Nov 1, 2023
@elichad
Copy link
Contributor Author

elichad commented Nov 1, 2023

The key concept here is known as idempotence/idempotency.

I'm roughly following this article to implement this for our Create* views: Idempotence and POST Requests in Django. This should be a bit more robust than disabling buttons on submit.

Progress on branch feature/2511-Double-clicking-submit-button-creates-duplicate-records

Extra info on an implementation in a major API: https://stripe.com/docs/api/idempotent_requests

Current status:

  • implement a mixin to make create views idempotent
  • implement middleware to catch duplicate requests
  • figure out what response to return that results in the user seeing the same as they usually would
  • create template tag to add idempotence tokens to POST forms, like CSRF tokens
  • ensure all views work as before
  • write plenty of tests

@elichad
Copy link
Contributor Author

elichad commented Dec 1, 2023

There is a lazier option here, which is to insert a bit of JS everywhere to disable submit buttons on submit. But the solution described above would work for both web and API requests, whereas JS works for web only. Disabling buttons is also not great for accessibility (though in this context, it's not too bad).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Miscellaneous issue backlog
To do - High priority
Development

No branches or pull requests

2 participants