Flask 內建的 session 功能
本文的參考專案 https://github.com/shinder/flask-practice
- 使用 session 時,需要設定 cookie 的加密字串(secret key)。
- 取得 session 值,使用
session.get('鍵')
。 - 設定 session 值,使用
session['鍵']=值
。
from flask import session
from datetime import timedelta
import os
app.config['SECRET_KEY'] = os.urandom(24)
app.config['PERMANENT_SESSION_LIFETIME'] = timedelta(minutes=60)
@app.route('/try-session')
def try_session():
if not session.get('what'):
session['what'] = 1
else:
session['what'] += 1
return str( session.get('what') )
沒有留言:
張貼留言