@Fil232323

Flask, как получить доступ к контексту приложение из другого модуля не в функии?

Есть структура файлов
testapp.py
....| - testfolder
........|-testfile.py

testapp.py
from flask import Flask
from testfolder.testfile import testmodule

app = Flask(__name__)
app.register_blueprint(testmodule, url_prefix='/test')    

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


testfile.py
from flask import Blueprint, current_app as app

testmodule = Blueprint('testmodule', __name__)

app.config['test '] = '123'

@testmodule.route('/')    
def index():
    print(app.config['test'])
    return "request is ok"


При попытке запуска приложения появляется ошибка
app.config['test '] = '123'
^^^^^^^^^^
RuntimeError: Working outside of application context.

This typically means that you attempted to use functionality that needed
the current application. To solve this, set up an application context
with app.app_context(). See the documentation for more information.


я понимаю что параметр контекста задаю не так , вопрос как нужно задать чтобы работало?
  • Вопрос задан
  • 137 просмотров
Пригласить эксперта
Ответы на вопрос 1
weranda
@weranda
А есть использовать переменную current_app контекста приложения, это исправит ситуацию?
Ответ написан
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы