Search code examples
djangodjango-formsrefresh

Prevent form resubmission with user refresh and need to submit data to webpage using Django


I've seen this question a few times and can't find an option that works in my situation.

I have a webpage that you can get to via a POST. It requires an 'example_id' be sent to the server.

The webpage has a form for the user to fill out. When they submit the form, I need the user to return to same page.

I need to prevent user 'refresh' from resubmitting the form. Most common solution I have found is:

return HttpResponseRedirect('/<web_page/')

I have tried this and adding kargs to the function parameters, but it doesn't seem to work.

Update: I got my scenario to work by using the csrf token ({% csrf_token %}) . If the user refreshes the page, then the csrf token will be the same. So I check for this in my view and handle this scenario differently.


Solution

  • The solution I found.

    I got my scenario to work by using the csrf token: {% csrf_token %}

    If the user refreshes the page, then the csrf token will be the same. So I check for this in my view and handle this scenario differently than I would normally.