0

After user submits I redirect back to the same page with the users submitted data in the form. I can see the data POST and then immediately followed by a GET of the same page in Firefox 36.0.4 desktop, Chrome 41.0.2272 desktop, Opera 10.0 iPhone 5, Chrome 41.0.2272 iPhone 5. However, only the Chrome on mobile causes a "Confirm Resubmit" on refresh and also doesn't load all the js again. Is there a fix for this problem?

Here is my minimal simple view.

def workout(request):
    form = WorkoutInfoForm(request.POST or None)
    if form.is_valid():
        form.save()
        post = request.POST.copy()
        request.session['_old_post'] = post
        return redirect(reverse("workout.views.workout"))
    if not request.POST:
        old_post = request.session.get('_old_post')
        form = WorkoutInfoForm(old_post or None)
    return render_to_response("workout/track_workout.html",
                              locals(),
                              context_instance=RequestContext(request))

And my form start in my template <form method='POST' action='' class="form-horizontal" role="form"> {% csrf_token %}

Colin
  • 271
  • 3
  • 7
  • possible duplicate of [Preventing form resubmission](http://stackoverflow.com/questions/3923904/preventing-form-resubmission) – rnevius Mar 28 '15 at 06:21
  • Thanks for the suggestion. The duplicate was not helpful since I am already in a POST/REDIRECT/GET mode. Even in GET I still have the refresh resubmit problem. Perhaps I am not using redirect correctly. – Colin Mar 28 '15 at 14:18

1 Answers1

1

If the form is valid your code works as expected. The described problem occures only in case of invalid form. You can try something like

def workout(request):
    form = WorkoutInfoForm(request.POST or None)
    if request.POST:
        post = request.POST.copy()
        request.session['_old_post'] = post
        if form.is_valid():
            form.save()
        return redirect(reverse("workout.views.workout"))
    else:
        old_post = request.session.get('_old_post')
        form = WorkoutInfoForm(old_post or None)
    return render_to_response("workout/track_workout.html",
                              locals(),
                              context_instance=RequestContext(request))
Thran
  • 1,111
  • 8
  • 10
  • Thanks for the suggestion. I changed my code to reflect your suggestion. On my laptop running Firefox 36.0.4 and Chrome 41.0.2272 the code works correctly for refresh using your code or my original code. When I try either code on my iPhone 5 both work correctly on Opera 10. However, running Chrome 41.0.2272 on the phone the refresh causes the "Confirm Resubmit". I'll keep your suggestion anyway. – Colin Mar 29 '15 at 13:50