在编辑记录时,如果等待几分钟(喝咖啡)然后再回来按下保存(POST),我会被重定向到主页面而不是登录而数据丢失.
似乎烧瓶登录会话过期太快了.
我做了一些研究并遇到了这个问题.
from flask import session,app session.permanent = True
这是正确的方法吗?但即使我尝试这个,我得到这个例外:
File "/Users/kave/workspace/F11A/src/application/__init__.py",line 14,in <module> session.permanent = True File "/Users/kave/workspace/F11A/src/lib/werkzeug/local.py",line 355,in <lambda> __setattr__ = lambda x,n,v: setattr(x._get_current_object(),v) File "/Users/kave/workspace/F11A/src/lib/werkzeug/local.py",line 297,in _get_current_object return self.__local() File "/Users/kave/workspace/F11A/src/lib/flask/globals.py",line 20,in _lookup_req_object raise RuntimeError('working outside of request context') RuntimeError: working outside of request context
解决方法
以防万一其他人会有这个问题.我想Hooman已经得到了答案.
不会工作
views.py
from flask import session from datetime import timedelta session.permanent = True app.permanent_session_lifetime = timedelta(minutes=30)
将工作
from flask import session from datetime import timedelta @app.route('/home',methods=['GET','POST']) def show_work(): session.permanent = True app.permanent_session_lifetime = timedelta(minutes=30) form = MyForm(request.form) return render_template('home.html',form = form)
会话必须在请求中使用.