refreshing a page loaded via POST

3,618 views
Skip to first unread message

mohammed safeer.mtp

unread,
Sep 14, 2011, 3:12:33 AM9/14/11
to django...@googlegroups.com
if a user hits “Refresh” on a page that was loaded via POST, that request is be repeated.
is there any remedy for this problem??

Tom Evans

unread,
Sep 14, 2011, 7:24:47 AM9/14/11
to django...@googlegroups.com

Yes, don't load the page via POST.

POST requests are meant to be used for data modifying requests, which
is why all user agents prompt you whether you really want to resubmit
the request. GET requests are not meant to be used for data modifying
requests, which is why they don't prompt you.

Cheers

Tom

Micah Carrick

unread,
Sep 14, 2011, 7:45:21 AM9/14/11
to django...@googlegroups.com
After you successfully process a POST request, you can use a redirect to send the user back to the orginal page.


--
You received this message because you are subscribed to the Google Groups "Django users" group.
To post to this group, send email to django...@googlegroups.com.
To unsubscribe from this group, send email to django-users...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.


Jacob Kaplan-Moss

unread,
Sep 14, 2011, 9:14:19 AM9/14/11
to django...@googlegroups.com

No, you can't prevent a web browser from re-POSTing forms.

What you can do is prevent the browser from ever staying on a
POST-generated page: the standard best practice is a technique called
"Post/Redirect/Get": see
http://en.wikipedia.org/wiki/Post/Redirect/Get for a high-level
description.

In Django, this translates to view functions that look like::

from django.shortcuts import redirect, render

def my_post_view(request):
if request.method == 'POST'
# do something here...
return redirect('some_other_view')
else:
# the request was a GET, so render a template:
return render(request, 'some/template.html')

The key part here to get is that you should always issue a redirect
after a successful POST request.

Jacob

Reply all
Reply to author
Forward
0 new messages