0

我想在我的应用程序中使用 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.
4

1 回答 1

-1

您需要先初始化您的类“current_app”,然后才能实际使用它。

current_app = Flask(__name__)
于 2017-02-07T21:41:03.603 回答