我正在写一个简单的Flask应用程序,第一页将要求用户名/密码,并检查有效性,然后更新session.logged_in为真。我使用打印输出进行检查,session.logged_in实际上设置为"True“。登录后,将有一个html页面,其中包含指向其他一些链接的导航,例如:表格列表( html文件中的列表具有链接{{ url_for('table_list') }}。但是,当我使用foreman并单击该链接时,session.logged_in为None (不是True或False)
我的python代码是:
from flask import Flask, stream_with_context, request, Response, url_for, render_template, flash, session
...
app = Flask(__name__)
app.debug = True # Enable debug-mode
app.secret_key = 'nosecret'
def stream_template(template_name, **context):
app.update_template_context(context)
t = app.jinja_env.get_template(template_name)
rv = t.stream(context)
# uncomment if you don't need immediate reaction
##rv.enable_buffering(5)
return rv
@app.route('/')
def index():
return render_template('signin.html')
@app.route('/', methods=['POST'])
def login():
# Read from the from input
usrn = request.form['username']
pswd = request.form['password']
if validate_login(usrn, pswd):
session['logged_in'] = True
print 'login1:', session.logged_in
sys.stdout.flush()
return Response(stream_with_context(stream_template('index.html', data=genData())))
else:
return Response(stream_template('fail.html', code=pswd, username=usrn))
.....
@app.route('/listtable')
def list_table():
print 'login2:', session.logged_in
sys.stdout.flush()
return Response(stream_with_context(stream_template('listtables.html', loginsession=session.logged_in)))控制台中的结果是:
login1: True
login2: none我认为问题是因为我使用上下文进行流式传输,以某种方式中断了我目前拥有的会话。有谁有办法解决这个问题吗?非常感谢。
解决方法:不确定我的问题是什么,但是我创建了一个名为MySession的新类,并且在我的会话中有一个布尔值来保存logged_in状态。通过使用MySession类,我可以更新我当前拥有的当前会话和信息,我不确定为什么Flask会话对我无效
https://stackoverflow.com/questions/22847547
复制相似问题