[Answer]-Django csrf in ajax not work

1👍

You should prevent double submission by taking care to properly
lay out your script execution flow & script structure so that you prevent that.

0👍

No, the CSRF token doesn’t prevent duplicate submit of any kind. Its purpose is to prevent Cross Site Request Forgery, nothing else. It creates a token so nobody can trick you in submitting requests you don’t intend to do.

If you want to prevent duplicate submits, a way would be to disable the submit button after it is clicked once. However, this is by no means a good solution, since JavaScript runs on client side and can easily be manipulated (e.g. via Firebug). So duplicate submits would still be possible, just not that obviously.

A better way is to do validation in your server-side Python code. You can check if the submitted data is already in the database and, if so, ignore the request or optionally return an error message. This makes sure that even by fiddling around with the JavaScript, an evil-meaning user cannot save data twice.

I would use both of these means, the first one simply to tell the user that he should not try to submit the same data twice – that’s just an interface perk.

👤j0ker

Leave a comment