How to be able to avoid a browser refreshing a page, after a HTTP POST request has been sent in Python/CGI
I am writing a CGI script in python 2.5 and I have run into an issue I cant solve.
My cgi script allows a user to into data into a html form and press refresh and the data gets successfully added. But it has turned out that if a user presses the refresh button on their browser the data that they inputted before gets added again. This is not the behaviour that 开发者_开发技巧I am looking for, and as such I would love to be able to redirect a user after a HTTP POST/GET request back to the main page.
This is of course naturally possible, by using the :
print "Location: www.website.here"
line in python. But I cannot do this as I need to be able to store messages between each refresh in order to be able to display information to the user.
These messages are essentially validation error messages, and if a user inputs wrong data and presses submit then my page should reload and print the error messages. I have this side of things working, but if I need to redirect using the above line, then I loose this.
So my question is are there any other possible ways to remove the option to refresh a html page using CGI and python, or am I looking at this all wrong and should I be trying to find a way to store my messages after a HTML redirect?
I assume you actually have two scenarios:
- The user entered the data correctly; reloading the page would cause the added data to be added again (which is wrong); the user may be redirected to the start page.
- The user did not enter the data correctly (there are error messages); reloading would cause the same message to be displayed again; redirect unacceptable.
I suppose you can issue a redirect only if the data validates, and display error messages in the other case.
If you have an option to migrate your CGI script to a modern web framework like Flask, Bottle, Django or Pyramid, I suggest you do so. This will give you session state and generally more convenient and modern environment.
A common way to do this is to store the pending messages for a given user on the server, associated with that user's session, and to display them the next time you deliver a page to that user.
Such messages are often called "Flash messages" (nothing to do with Adobe Flash) or "Session messages".
See the Django documentation for an idea of how you would do this in Django.
Your question is extremely confusing, because you say “press refresh” adds data. – You also don’t mention if your form is submitted via POST or GET (is it submitted at all??).
There is nothing that protects you from double entries (the user can always hit reload) if you don’t guard actively against them. One way is to deliver your form with some unique number and check if you already accepted submissions for that number.
And the other problem you face is as old as HTTP itself: session state. If you don’t want to start with session IDs or cookies, but need to carry your data through some pages, add all state to the URL all the time.
The new approach is to use AJAX, where your submit button is not going to submit classically anymore, but instead is going to execute some JavaScript in which you submit on a different (logical) connection, wait for the result and than change something in the still existing HTML page so that the user can see the result (e.g. adding a paragraph at a prominent place), but has the form still in front of him as is.
精彩评论