Tuesday, July 31, 2012

Redirection is good after Form submit, to prevent "Reload button - double submission" problem.


http://stackoverflow.com/questions/5823580/django-form-resubmitted-upon-refresh


After successful submission and processing of a web form, you need to use a returnHttpResponseRedirect, even if you are only redirecting to the same view. Otherwise, certain browsers (I'm pretty sure FireFox does this) will end up submitting the form twice.



Here's an example of how to handle this...
def some_view(request):
  if request.method == "POST":
    form = some_form(request.POST)
    if form.is_valid():
      # do processing
      # save model, etc.
      return HttpResponseRedirect("/some/url/")
  return render_to_response("normal/template.html", {"form":form}, context_instance=RequestContext(request))


###############
The asker's scenario:


After I submit the form for the first time and then refresh the form it gets resubmitted and and I don't want that.
Here's my form in template :

No comments:

Post a Comment