Last active
June 8, 2019 12:20
-
-
Save Drvanon/54e07510e078f73bee42d186dbd26448 to your computer and use it in GitHub Desktop.
Flask Context Problem
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from flask import Flask | |
def create_app(test_config=None): | |
# create and configure the app | |
app = Flask(__name__ ) | |
app.config.from_mapping( SECRET_KEY='12398fjnkcm,zxcjnd123n') | |
from . import auth | |
app.register_blueprint(auth.bp) | |
return app |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from functools import wraps | |
from flask import ( | |
Blueprint, g, request, session, jsonify, abort, current_app | |
) | |
bp = Blueprint('auth', __name__) | |
@bp.before_app_request | |
def load_logged_in_user(): | |
user_id = session.get('user_id') | |
if user_id is None: | |
g.user = None | |
else: | |
g.user = 'some text' | |
def require_authorization(f): | |
with current_app.app_context(): | |
@wraps(f) | |
def decorator(*args, **kwargs): | |
if 'user' in g and g.user: | |
return f(*args, **kwargs) | |
abort(401) | |
return decorator | |
@bp.route('/my-url') | |
@require_authorization | |
def my_test_function(): | |
return jsonify({"message":"success"}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Traceback (most recent call last): | |
File "/home/robin/.local/share/virtualenvs/angular-todo-ZjYtNydN/lib/python3.7/site-packages/flask/_compat.py", line 35, in reraise | |
raise value | |
File "/home/robin/.local/share/virtualenvs/angular-todo-ZjYtNydN/lib/python3.7/site-packages/flask/cli.py", line 76, in find_best_app | |
app = call_factory(script_info, app_factory) | |
File "/home/robin/.local/share/virtualenvs/angular-todo-ZjYtNydN/lib/python3.7/site-packages/flask/cli.py", line 116, in call_factory | |
return app_factory() | |
File "/home/robin/Documents/projects/learning/angular-todo/api/__init__.py", | |
line 45, in create_app | |
from . import todos | |
File "/home/robin/Documents/projects/learning/angular-todo/api/todos.py", line 87, in <module> | |
@get_todolist | |
File "/home/robin/Documents/projects/learning/angular-todo/api/auth.py", line 98, in decorator | |
if 'user' in g and g.user: | |
File "/home/robin/.local/share/virtualenvs/angular-todo-ZjYtNydN/lib/python3.7/site-packages/werkzeug/local.py", line 379, in <lambda> | |
__contains__ = lambda x, i: i in x._get_current_object() | |
File "/home/robin/.local/share/virtualenvs/angular-todo-ZjYtNydN/lib/python3.7/site-packages/werkzeug/local.py", line 306, in _get_current_object | |
return self.__local() | |
File "/home/robin/.local/share/virtualenvs/angular-todo-ZjYtNydN/lib/python3.7/site-packages/flask/globals.py", line 44, in _lookup_app_object | |
raise RuntimeError(_app_ctx_err_msg) | |
RuntimeError: Working outside of application context. | |
This typically means that you attempted to use functionality that needed | |
to interface with the current application object in some way. To solve | |
this, set up an application context with app.app_context(). See the | |
documentation for more information. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
How did you solve the context issue? I've bumped into the same.