Flask-JWT似乎无法识别current_app

时间:2016-08-01 15:56:58

标签: python flask flask-jwt

我想在我的应用中使用Flask-JWT。当我尝试使用current_app初始化它时,出现错误。到目前为止,我已成功使用current_app代替app = Flask(__name__),所以这有点出乎意料。有什么我想念的吗?

main.py

from flask import Flask

app = Flask(__name__)
app.config.from_pyfile("./config.py")

from app import api_auth
app.register_blueprint(api_auth.bp)

api_auth.py

from flask import current_app
from flask_jwt import JWT

def authenticate(username, password):
  ...
  return user

def identity(payload):
  ...
  return userid_table.get(user_id, None)

jwt = JWT(current_app, authenticate, identity)
Traceback (most recent call last):
  File "./wsgi.py", line 1, in <module>
    from main import app as application
  File "./main.py", line 5, in <module>
    from auth import api_auth
  File "./auth.py", line 59, in <module>
    jwt = JWT(current_app, authenticate, identity)
  File "/opt/mist_base/env/lib/python2.7/site-packages/flask_jwt/__init__.py", line 216, in __init__
    self.init_app(app)
  File "/opt/mist_base/env/lib/python2.7/site-packages/flask_jwt/__init__.py", line 220, in init_app
    app.config.setdefault(k, v)
  File "/opt/mist_base/env/lib/python2.7/site-packages/werkzeug/local.py", line 343, in __getattr__
    return getattr(self._get_current_object(), name)
  File "/opt/mist_base/env/lib/python2.7/site-packages/werkzeug/local.py", line 302, in _get_current_object
    return self.__local()
  File "/opt/mist_base/env/lib/python2.7/site-packages/flask/globals.py", line 51, in _find_app
    raise RuntimeError(_app_ctx_err_msg)
RuntimeError: Working outside of application context.

1 个答案:

答案 0 :(得分:0)

您需要在实际使用之前初始化您的类'current_app'。

current_app = Flask(__name__)