Estoy tratando de construir un blog como una muestra de cartera usando python3 y matraz y matraz_jwt_extendido.
Puedo crear un solo archivo como este y se ejecutará:
from flask_jwt_extended import (create_access_token, get_jwt_identity, JWTManager, jwt_required, get_raw_jwt) from flask import Flask, request, Blueprint app = Flask(__name__) app.config['JWT_SECRET_KEY'] = 'this-is-super-secret' app.config['JWT_BLACKLIST_ENABLED'] = True app.config['JWT_BLACKLIST_TOKEN_CHECKS'] = ['access'] jwt = JWTManager(app) @app.route(....) @jwt requiredPero cuando trato de usar Blueprint, no registrará el JWTManager
Aquí está mi archivo user.py:
from flask_jwt_extended import (create_access_token, get_jwt_identity, JWTManager, jwt_required, get_raw_jwt) from flask import Flask, request, Blueprint app = Flask(__name__) app.config['JWT_SECRET_KEY'] = 'this-is-super-secret' app.config['JWT_BLACKLIST_ENABLED'] = True app.config['JWT_BLACKLIST_TOKEN_CHECKS'] = ['access'] jwt = JWTManager(app) user_blueprint = Blueprint('user_blueprint', __name__) @user_blueprint.route(....) @jwt requiredaquí está mi aplicación.py:
from user import * app = Flask(__name__) app.register_blueprint(user_blueprint)Ahora, cuando intento ejecutar app.py, devuelve un 500 (Error interno) y registrará esto en el archivo de registro:
Traceback (most recent call last): File "/usr/local/lib/python3.6/dist-packages/flask_jwt_extended/utils.py", line 127, in _get_jwt_manager return current_app.extensions['flask-jwt-extended'] KeyError: 'flask-jwt-extended' During handling of the above exception, another exception occurred: Traceback (most recent call last): File "/usr/local/lib/python3.6/dist-packages/flask/app.py", line 2292, in wsgi_app response = self.full_dispatch_request() File "/usr/local/lib/python3.6/dist-packages/flask/app.py", line 1815, in full_dispatch_request rv = self.handle_user_exception(e) File "/usr/local/lib/python3.6/dist-packages/flask/app.py", line 1718, in handle_user_exception reraise(exc_type, exc_value, tb) File "/usr/local/lib/python3.6/dist-packages/flask/_compat.py", line 35, in reraise raise value File "/usr/local/lib/python3.6/dist-packages/flask/app.py", line 1813, in full_dispatch_request rv = self.dispatch_request() File "/usr/local/lib/python3.6/dist-packages/flask/app.py", line 1799, in dispatch_request return self.view_functions[rule.endpoint](**req.view_args) File "/home/ali/Desktop/cetia_api/user.py", line 63, in login return create_token(user_inputs) File "/home/ali/Desktop/cetia_api/user_functions.py", line 103, in create_token access_token = create_access_token(identity=data, expires_delta=expires) File "/usr/local/lib/python3.6/dist-packages/flask_jwt_extended/utils.py", line 156, in create_access_token jwt_manager = _get_jwt_manager() File "/usr/local/lib/python3.6/dist-packages/flask_jwt_extended/utils.py", line 129, in _get_jwt_manager raise RuntimeError("You must initialize a JWTManager with this flask " RuntimeError: You must initialize a JWTManager with this flask application before using this method¿Podría alguien decirme qué hacer? Probé TODO durante los últimos 3 días. han pasado 20 horas de depuración y todavía no está arreglado
Necesitas usar la misma app en cada ubicación. Actualmente está creando una app en su archivo users.py y una app diferente en su archivo app.py.
Por lo general, querrá usar el patrón de fábrica de aplicaciones de matraz para hacer esto ( https://flask.palletsprojects.com/en/1.1.x/patterns/appfactories/ ). Un ejemplo podría verse así:
from flask_jwt_extended import JWTManager jwt = JWTManager() from flask_jwt_extended import (create_access_token, get_jwt_identity, jwt_required, get_raw_jwt) from flask import Flask, request, Blueprint user_blueprint = Blueprint('user_blueprint', __name__) @user_blueprint.route(....) @jwt_required from extensions import jwt from users import users_blueprint def create_app(): app = Flask(__name__) app.secret_key = 'ChangeMe!' app.config['JWT_BLACKLIST_ENABLED'] = True app.config['JWT_BLACKLIST_TOKEN_CHECKS'] = ['access'] jwt.init_app(app) app.register_blueprint(user_blueprint) return app from app import create_app app = create_app() if __name__ == '__main__': app.run()