我正在尝试创建一个在谷歌应用引擎上运行的网络应用。我正在尝试使用Flask-Login实现身份验证。 当我运行应用程序时,我收到如下所述的错误消息。
我已经安装了Flask-Login和所有其他库。
如果您在此处需要更多信息,请与我们联系。
请帮帮我。这让我发疯了。
MyServices.py
import os, sys
on_appengine = os.environ.get('SERVER_SOFTWARE','').startswith('Development')
if on_appengine and os.name == 'nt':
sys.platform = "Not Windows"
sys.path.insert(1, os.path.join(os.path.abspath('.'), 'lib'))
from lib.flask import Flask
from lib.flask_login import LoginManager
app = Flask(__name__)
app.config.from_pyfile('app.config')
login_manager = LoginManager()
login_manager.init_app(app)
import MyApp
MyApp.register_blueprints(app)
MyApp.py
from app.controllers.auth import auth_bp
from app.controllers.home import home_bp
def register_blueprints(app):
app.register_blueprint(home_bp)
app.register_blueprint(auth_bp)
home.py
from lib.flask import render_template, Blueprint
home_bp = Blueprint("home", __name__, template_folder='../templates')
@home_bp.route('/')
def Get():
return render_template('home/home.html')
auth.py
from app.Repository.UserInfoRepository import UserInfoRepository
from lib.flask import render_template, Blueprint, session, request, make_response, redirect, flash
from lib.flask_login import current_user, login_required, login_user, logout_user, confirm_login, fresh_login_required
from MyServices import login_manager
auth_bp = Blueprint("auth", __name__, template_folder='../templates')
@auth_bp.route('/login', methods=['POST'])
def login():
userInfoRepository = UserInfoRepository()
user = userInfoRepository.AuthenticateUser(request)
if user is not None and user.is_authenticated:
if login_user(user, remember=True):
flash("Logged in!")
return render_template('home/home.html', name=current_user)
else:
flash("unable to log you in")
return render_template('home/home.html')
def IsLoggedIn():
if not session.has_key('uid'):
return False
return session['ip'] == request.remote_addr
@auth_bp.route('/logout')
@login_required
def logout():
logout_user()
return render_template('home/home.html')
@login_manager.unauthorized_handler
def unauthorized_callback():
return redirect('/')
@login_manager.user_loader
def load_user(id):
if id is None:
redirect('/')
userInfoRepository = UserInfoRepository()
user = userInfoRepository.GetUserByID(id)
if user.is_active():
return user
else:
return None
错误消息
[2017-09-15 21:11:47,875] ERROR in app: Exception on / [GET]
Traceback (most recent call last):
File "D:\PyCharmProjects\myservices\lib\flask\app.py", line 1982, in wsgi_app
response = self.full_dispatch_request()
File "D:\PyCharmProjects\myservices\lib\flask\app.py", line 1615, in full_dispatch_request
return self.finalize_request(rv)
File "D:\PyCharmProjects\myservices\lib\flask\app.py", line 1632, in finalize_request
response = self.process_response(response)
File "D:\PyCharmProjects\myservices\lib\flask\app.py", line 1856, in process_response
response = handler(response)
File "D:\PyCharmProjects\myservices\lib\flask_login\login_manager.py", line 380, in _update_remember_cookie
if 'remember' in session:
File "D:\PyCharmProjects\myservices\lib\werkzeug\local.py", line 379, in <lambda>
__contains__ = lambda x, i: i in x._get_current_object()
File "D:\PyCharmProjects\myservices\lib\werkzeug\local.py", line 306, in _get_current_object
return self.__local()
File "D:\PyCharmProjects\myservices\lib\flask\globals.py", line 37, in _lookup_req_object
raise RuntimeError(_request_ctx_err_msg)
RuntimeError: Working outside of request context.
答案 0 :(得分:1)
最后,我能够找出问题所在。问题在于我从lib文件夹处理第三方软件包的方式。
我通过添加__init__.py将lib文件夹放入包中。我正在调用lib.flask来导入烧瓶。但是,flask-login模块导入flask而不是lib.flask。这就是为什么当flask-login模块调用会话变量时,他们从flask模块而不是lib.flask获取它。因此它脱离了背景。 解决方案是从lib文件夹中删除__init__.py。在搜索中添加lib文件夹,我已经通过添加 sys.path.insert(1,os.path.join(os.path.abspath(&#39;。&#39;),&# 39; LIB&#39;))即可。
如果您使用的是PyCharm,它会显示您没有找到导入烧瓶的错误。如果您的代码正常工作,可以忽略错误。
如果您有更多问题,请与我们联系。