问题
While editing a record, if there is a long wait of let say a few minutes (getting coffee) and then coming back to press the save (POST), I get redirected to the main page to login instead and the data is lost.
It seems the flask-login session expires too fast.
I did some research and came across this.
from flask import session, app
session.permanent = True
Is this the proper way to go? But even when I try this I get this exception:
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(), n, 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
回答1:
Just in case someone else will have this question. I suppose Hooman already got answer.
Won`t work
views.py
from flask import session
from datetime import timedelta
session.permanent = True
app.permanent_session_lifetime = timedelta(minutes=30)
Will work
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)
session must be used inside the request.
回答2:
If you try to access the session object like this it won't work.
As the error message says, flask.session can only be used from within a request context, which won't exist at that point. You should only use it from within a route.
来源:https://stackoverflow.com/questions/18662558/flask-login-session-times-out-too-soon