【发布时间】:2020-10-18 05:56:06
【问题描述】:
我正在尝试将 cookie 的过期时间设置为比浏览器会话长。我的config.py 是:
from datetime import timedelta
SESSION_FILE_DIR = 'C:/some/path15'
SECRET_KEY= 'abcdefg'
DEBUG = True
SESSION_PERMANENT = True
PERMANENT_SESSION_LIFETIME = timedelta(days=30)
然后为了模仿我的应用程序结构,我有一个注册蓝图的主应用程序:
from flask import Flask, render_template, request, session, current_app
from flask_session import Session
import tempfile
server = Flask(__name__)
server.config.from_pyfile('config.py')
### Import and Register Blueprints
from tools.routes import my_bp
server.register_blueprint(my_bp)
@server.route('/')
def homepage():
return "Hello"
if __name__ == '__main__':
server.run(debug=True)
然后是一个名为 routes.py 的蓝图,位于名为 tools 的主应用程序的子目录中
from flask import Flask, render_template, request, session, Blueprint, current_app
from flask_session import Session
import tempfile
my_bp = Blueprint("my_bp", __name__)
@my_bp.route('/new', methods=['POST', 'GET'])
def path():
if 'path' not in session: ##new
session['path'] = tempfile.mkdtemp() ##new
path = session['path'] ##new
return path
在运行这个应用程序时(通过 /new 路由),如果我在浏览器中检查 Storage 下的 Element,它会显示 cookie expire/max_age 是 Session。
我怎样才能让这个遵守我在 config.py 文件中设置的 30 天到期?
【问题讨论】:
标签: python python-3.x flask session-cookies