Flask 简明教程
Flask – Sessions
类似于 Cookie,会话数据存储在客户端。会话是指客户端登录到一个服务器并登出时的时段。在这段时间内需要保存的数据都存储在客户端浏览器中。
Like Cookie, Session data is stored on client. Session is the time interval when a client logs into a server and logs out of it. The data, which is needed to be held across this session, is stored in the client browser.
每个客户端的会话都会被分配一个 Session ID 。会话数据被存储在 Cookie 之上,并且服务器对其进行加密签名。对于这种加密,Flask 应用需要一个定义过的 SECRET_KEY 。
A session with each client is assigned a Session ID. The Session data is stored on top of cookies and the server signs them cryptographically. For this encryption, a Flask application needs a defined SECRET_KEY.
会话对象也是一个包含会话变量及其关联值的关键值对的对象字典。
Session object is also a dictionary object containing key-value pairs of session variables and associated values.
例如,要设置一个 ‘username’ 会话变量,请使用这条语句 −
For example, to set a ‘username’ session variable use the statement −
Session[‘username’] = ’admin’
要释放一个会话变量,请使用 pop() 方法。
To release a session variable use pop() method.
session.pop('username', None)
以下代码简单演示了会话在 Flask 中的工作。URL ‘/’ 仅仅提示用户登录,因为会话变量 ‘username’ 没有被设置。
The following code is a simple demonstration of session works in Flask. URL ‘/’ simply prompts user to log in, as session variable ‘username’ is not set.
@app.route('/')
def index():
if 'username' in session:
username = session['username']
return 'Logged in as ' + username + '<br>' + \
"<b><a href = '/logout'>click here to log out</a></b>"
return "You are not logged in <br><a href = '/login'></b>" + \
"click here to log in</b></a>"
当用户浏览到登录(login())视图函数的 ‘/login’ 时,因为它通过 GET 方法被调用,它会弹出一个登录表单。
As user browses to ‘/login’ the login() view function, because it is called through GET method, opens up a login form.
一个表单被提交回 ‘/login’ ,现在会话变量被设置。应用被重定向到 ‘/’ 。这一次找到了会话变量 ‘username’ 。
A Form is posted back to ‘/login’ and now session variable is set. Application is redirected to ‘/’. This time session variable ‘username’ is found.
@app.route('/login', methods = ['GET', 'POST'])
def login():
if request.method == 'POST':
session['username'] = request.form['username']
return redirect(url_for('index'))
return '''
<form action = "" method = "post">
<p><input type = text name = username/></p>
<p<<input type = submit value = Login/></p>
</form>
'''
应用程序还包含一个 logout() 视图函数,它弹出一个 ‘username’ 会话变量。因此, ‘/’ URL 再次显示打开页面。
The application also contains a logout() view function, which pops out ‘username’ session variable. Hence, ‘/’ URL again shows the opening page.
@app.route('/logout')
def logout():
# remove the username from the session if it is there
session.pop('username', None)
return redirect(url_for('index'))
运行应用程序并访问主页。(确保设置应用程序的 secret_key )
Run the application and visit the homepage. (Ensure to set secret_key of the application)
from flask import Flask, session, redirect, url_for, escape, request
app = Flask(__name__)
app.secret_key = 'any random string’
输出如下所示。单击链接 “click here to log in” 。
The output will be displayed as shown below. Click the link “click here to log in”.
链接将被定向到另一个屏幕。输入 ‘admin’。
The link will be directed to another screen. Type ‘admin’.
屏幕会显示给你一条消息, ‘Logged in as admin’ 。
The screen will show you the message, ‘Logged in as admin’.