Redirect (redirects) and user session
The hello.py we write today has a usability problem. If you enter name and submit, and then click the Refresh button, then the browser may pop up an inexplicable table alarm, asking you to confirm whether to resend the form data, in fact, you did nothing. This is because the browser's refresh operation actually repeats the last request sent, in which case the last request is to send a POST request with the form data. Obviously, everyone doesn't want this effect.
Many users do not understand the browser pop-up alarm. Therefore, one of the best practices for Web applications is to never let a POST request become the last request of the browser.
This practice can be implemented by using a response to a redirect
POST request. Redirection is a special response, and his content is a URL instead of an HTML string. When the browser receives this response, a GET request is sent to the URL, and the page is displayed. In this case, it takes a little longer to send two requset, but the experience has little impact. Now, the last request is get, and there's no warning if it refreshes. This technique is known as post/redirect/get mode.
Well, here's another problem. When the application processes the POST request, the form data is saved to the variable in the View function name
, and the value of the variable is lost after the function exits. Now that the post response is a redirect, you need to save the value.
The application can use the * user session to store data between Requset, which is private to each connected user. This user session is mentioned in the previous Requset context ( This is still a pit, not filled out *). It works like a dictionary.
Note: By default, the session is stored on the client side of the cookie and is signed by the previously configured SECRET_KEY
encryption. Any attempt to tamper with its contents will cause the signature to fail, eventually invalidating the session
To value the targeted version index()
:
From flask import Flask, render_template, session, redirect, Url_for@app.route ('/', methods=[' GET ', ' POST ']) def index (): form = Nameform () if Form.validate_on_submit (): session[' name '] = form.name.data return Redirect ( Url_for (' index ')) return render_template (' index.html ', Form=form, Name=session.get (' name '))
Note: Code 4b tag
Data that was previously saved with a temporary variable name
is now modified to be placed in so that the session
data can be saved between requset.
Now the function returns the redirect response in the case of data, which is redirect()
implemented by means of a method. The input to this method is the URL to redirect to, and here is the url_for()
function, which was previously mentioned in the template and automatically generates a URL based on the name of the entered view function. Write directly redirect(‘/‘)
here the effect is the same, but not recommended.
' Url_for () ' accepts only one parameter, which is the routing endpoint name, which is the name of the view function by default for a route, so it says index
.
Now the web work is normal.
Information Tips
Sometimes need to give users some information, error alarm and so on. For example, when the user logs in the wrong password, there is a message to prompt the user name or password error.
The Flask framework incorporates this functionality as a core feature. flash()
method is used to do this thing.
Examples of displaying messages:
From flask import Flask, render_template, session, redirect, Url_for, Flash@app.route ('/', methods=[' GET ', ' POST ']) def in Dex (): form = Nameform () if Form.validate_on_submit (): old_name = session.get (' name ') if Old_name is Not None and old_name! = Form.name.data: Flash (' Looks like you have changed your name! ') session[' name '] = form.name.data form.name.data = " return redirect" (Url_for (' index ')) return Render_ Template (' index.html ', form = form, name = Session.get (' name '))
Or hello.py, here we have added a flash()
use. When the user submits a different name two times, in our response, there is a message.
Only calls in the code flash()
, of course, cannot display the message, and the template needs to be modified. This change is best done in the base template so that no changes are needed for all the sub-templates. The contents of a message can be received in a template by get_flashed_messages()
means of a method.
Or an example, the base template templates/base.html
:
{% block content%}<div class= "container" > {% for message in get_flashed_messages ()%} <div class= " Alert Alert-warning "> <button type=" button "class=" Close "data-dismiss=" alert ">×</button > {message}} </div> {% endfor%} {% block page_content%}{% Endblock%}</div>{% Endblock%}
Note: Code 4c Tag
The above uses the loop structure supported by the template engine, each time it is called in the code, there is flash()
get_flashed_messages()
one more record, and the record cannot be acquired after it is extracted. The bootstrap alert CSS style is used for rendering. :
The beginning of the next section is the database.
Flaskwebdevelopment-handling of Forms 2-redirect & user session& Tips