Home > Software engineering >  Getting the error Working outside of application context
Getting the error Working outside of application context

Time:09-17

I have struggled with this and read several articles and none seem to work for me I have used "with app.app_context():" right after the Flask call and it does not work. I am trying to print out the context before it hits the route. Please help.

Traceback (most recent call last): File "/Users/user1/Documents/PycharmProjects/file_upload2/app/wsgi.py", line 7, in app = create_app() File "/Users/user1/Documents/PycharmProjects/file_upload2/app/flask_project/init.py", line 105, in create_app from .uploads import uploads File "/Users/user1/Documents/PycharmProjects/file_upload2/app/flask_project/uploads.py", line 13, in print(current_app.config.get("MAX_CONTENT_LENGTH")) File "/Users/user1/Documents/VENV/file_upload2/lib/python3.9/site-packages/werkzeug/local.py", line 316, in get obj = instance._get_current_object() # type: ignore[misc] File "/Users/user1/Documents/VENV/file_upload2/lib/python3.9/site-packages/werkzeug/local.py", line 513, in _get_current_object raise RuntimeError(unbound_message) from None RuntimeError: Working outside of application context.

################################################################################
# init.py                                                                      #
################################################################################
from flask import Flask

def create_app():
    app = Flask(__name__)

    # upload options
    file_mb_max = 100  # in MB
    # app.config['MAX_CONTENT_LENGTH'] = file_mb_max * 1024 * 1024  # tried this
    MAX_CONTENT_LENGTH = file_mb_max * 1024 * 1024

    # Register Blueprints
    from .uploads import uploads
    app.register_blueprint(uploads, url_prefix='/upload/')


    return app

################################################################################
# wsgi.py                                                                      #
################################################################################
from flask_project import create_app

app = create_app()

if __name__ == '__main__':
    app.run(debug=True)

################################################################################
# uploads.py                                                                   #
################################################################################
from flask import Flask, render_template, request, redirect, url_for, send_from_directory, Blueprint, flash, current_app

uploads = Blueprint('uploads', __name__)

# Check that the upload folder exists
print(current_app.config['MAX_CONTENT_LENGTH'])

CodePudding user response:

regarding the current_app you can read from documentation:

Flask automatically pushes an application context when handling a request. View functions, error handlers, and other functions that run during a request will have access to current_app.

If you would include the print(current_app.config['MAX_CONTENT_LENGTH']) in a view (which is a function that runs during a request), then it will work.

Example uploads.py with a route "home":

from flask import Flask, render_template, request, redirect, url_for, send_from_directory, Blueprint, flash, current_app

uploads = Blueprint('uploads', __name__)

@uploads.route("/")
def home():
    print(current_app.config['MAX_CONTENT_LENGTH'])
    return "OK"

To access it you need to navigate to path /upload/home of your web server.

Also the MAX_CONTENT_LENGTH should be set as app.config['var name here'] as you already tried and commented, your init.py:

def create_app():
    app = Flask(__name__)

    # upload options
    file_mb_max = 100  # in MB
    app.config['MAX_CONTENT_LENGTH'] = file_mb_max * 1024 * 1024

    # Register Blueprints
    from .uploads import uploads
    app.register_blueprint(uploads, url_prefix='/upload/')


    return app

CodePudding user response:

register your blueprints after create_app() with app.context

app = create_app()
with app.app_context():
    from .uploads import uploads
    app.register_blueprint(uploads, url_prefix='/upload')
  • Related