Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

django form resubmitted upon refresh

Tags:

forms

django

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 :

<form action = "" method = "POST"> {% csrf_token %}     {{ form.as_p }}     <input type = "submit" value = "Shout!"/> </form> 

How can I fix this ?

Here's my views:

def index(request):     shouts = Shout.objects.all()      if request.method == "POST":         form = GuestBookForm(request.POST)         if form.is_valid():             cd = form.cleaned_data             shout = Shout(author = cd['author'], message = cd['message'])             shout.save()             form = GuestBookForm()     else:         form = GuestBookForm()      return render_to_response('guestbook/index.html', {'shouts' : shouts,                                              'form' : form },                               context_instance = RequestContext(request)) 
like image 762
Marijus Avatar asked Apr 28 '11 18:04

Marijus


People also ask

How do you stop resubmission when a page is refreshed Django?

One way to stop page resubmission on page refresh is to unset the form data after it is submitted so that the variable storing form data becomes empty and wrap up your form processing block of codes to check if the form is empty.

How do I stop form resubmission when a page is refreshed?

You can prevent form resubmission via a session variable. Yes we can use microtime() as well as time() also instead of rand() , whatever function or variable that gives different value we can use it. BUT make sure that you set that value to SESSION variable.


1 Answers

My guess is that this is a problem in your view.

After successful submission and processing of a web form, you need to use a return HttpResponseRedirect, 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)) 

Given your recently added view above...

def index(request):     shouts = Shout.objects.all()      if request.method == "POST":         form = GuestBookForm(request.POST)         if form.is_valid():             cd = form.cleaned_data             shout = Shout(author = cd['author'], message = cd['message'])             shout.save()              # Redirect to THIS view, assuming it lives in 'some app'             return HttpResponseRedirect(reverse("some_app.views.index"))     else:         form = GuestBookForm()      return render_to_response('guestbook/index.html', {'shouts' : shouts,                                          'form' : form },                           context_instance = RequestContext(request)) 

That will use reverse to redirect to this same view (if thats what you are trying to do)

like image 95
Brant Avatar answered Oct 04 '22 02:10

Brant