Flask: login session times out too soon

2024/11/18 1:25:44

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, appsession.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 = TrueFile "/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_objectreturn self.__local()File "/Users/kave/workspace/F11A/src/lib/flask/globals.py", line 20, in _lookup_req_objectraise RuntimeError('working outside of request context')
RuntimeError: working outside of request context
Answer

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 timedeltasession.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 = Trueapp.permanent_session_lifetime = timedelta(minutes=30)form = MyForm(request.form)return render_template('home.html', form = form)

session must be used inside the request.

https://en.xdnf.cn/q/71116.html

Related Q&A

Activate virtual environement and start jupyter notebook all in batch file

I created the following batch file: jupyter_nn.bat. Inside file I have:cd "C:\My_favorite_path" activate neuralnets jupyter notebookSo the goal is to activate conda virtual environment and s…

several contour plots in the same figures

I have several 3d functions. I would like two plot the contour plots of them in the same figure to see the difference between them. I expect to see some crossings between contours of two functions. Her…

how to detect all the rectangular boxes in the given image

I tried to detect all the rectangles in image using threshold, canny edge and applied contour detection but it was not able to detect all the rectangles. Finally, I thought of detect the same using hou…

Python Pandas Series failure datetime

I think that this has to be a failure of pandas, having a pandas Series (v.18.1 and 19 too), if I assign a date to the Series, the first time it is added as int (error), the second time it is added as …

Remove a dictionary key that has a certain value [duplicate]

This question already has answers here:Removing entries from a dictionary based on values(4 answers)Closed 10 years ago.I know dictionarys are not meant to be used this way, so there is no built in fun…

Get names of positional arguments from functions signature

Using Python 3.x, Im trying to get the name of all positional arguments from some function i.e: def foo(a, b, c=1):returnRight now Im doing this: from inspect import signature, _empty args =[x for x, p…

Emacs Python-mode syntax highlighting

I have GNU Emacs 23 (package emacs23) installed on an Ubuntu 10.04 desktop machine and package emacs23-nox installed on an Ubuntu 10.04 headless server (no X installed). Both installations have the sam…

programmatically edit tab order in pyqt4 python

I have a multiple textfield in my form. My problem is the tab order is wrong. Is there a way to edit tab order in code? Just like in QT Designer.thanks.

Getting the parent of a parent widget in Python Tkinter

I am trying to get the parent of a widget then get the parent of that widget. But Everytime I try to I get a error.Error:AttributeError: str object has no attribute _nametowidgetWhy is it giving me tha…

Python Random List Comprehension

I have a list similar to:[1 2 1 4 5 2 3 2 4 5 3 1 4 2] I want to create a list of x random elements from this list where none of the chosen elements are the same. The difficult part is that I would lik…