【发布时间】:2020-11-30 11:22:31
【问题描述】:
在 Flask 应用程序中(在 __init__.py 中初始化)我有两个蓝图 - auth 和 main 。在auth 蓝图中,我试图设置一些变量(将从数据库加载并依赖于current_user.get_id()),这些变量应该在main 蓝图中用作url-prefix:
auth.py
@auth.route('/login', methods=['POST'])
def login_post():
username = request.form.get('username')
password = request.form.get('password')
user_inst = user.query.filter_by(username=username).first()
if not user_inst or not check_password_hash(user_inst.password, password):
flash('Invalid credentials. Check you input and try again.')
return redirect(url_for('auth.login'))
login_user(user_inst)
g.team_name = 'some_team_name'
#session['team_name'] = 'some_team_name'
# if the above check passes, then we know the user has the right credentials
return redirect(url_for('main.some_func'))
在主蓝图中,需要获取team_name变量:
main = Blueprint('main', __name__, static_folder="static", static_url_path="", url_prefix=g.team_name)
您能否告知是否有适当的方法将变量从auth 导入到main(在其初始化之前)而无需获取:
RuntimeError: Working outside of application context.
【问题讨论】: