我正在学习第7章中的'flask_web_development',它将应用程序封装到工厂中
def create_app(config_name):
app = flask(__name__)
app.config.from_object(config[config_name])
config[config_name].init_app(app)
bootstrap.init_app(app)
mail.init_app(app)
moment.init_app(app)
db.init_app(app)
from .main import main as main_blueprint
app.register_blueprint(main_blueprint)
return app
运行烧瓶壳时报告错误
In [21]: !flask shell
Usage: flask shell [OPTIONS] [IPYTHON_ARGS]...
Error: Failed to find flask application or factory in module "flask". Use "flaSK_APP=flask:name to specify one.
烧瓶运行正常
In [19]: !export flaSK_APP=flask.py; export flaSK_DEBUG=1
In [20]: !flask run &
In [21]: * Serving flask app "flask.py" (lazy loading)
* Environment: production
WARNING: This is a development server. Do not use it in a production deployment.
Use a production WSGI server instead.
* Debug mode: on
* Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)
* Restarting with stat
* Debugger is active!
* Debugger PIN: 301-086-427
flask shell
在前面的章节中运行良好,我将其与`flask-shell-ipython'设置为ipython
详细信息
In [26]: !export flaSK_APP=flasky.py
In [27]: !flask shell
Usage: flask shell [OPTIONS] [IPYTHON_ARGS]...
Error: Failed to find flask application or factory in module "flask". Use "flaSK_APP=flask:name to specify one.
和flasky.py
In [28]: !cat flasky.py
import os
import click
from flask_migrate import Migrate
from app import create_app,db
from app.models import User,Role
app = create_app(os.getenv('flaSK_CONFIG') or 'default')
migrate = Migrate(app,db)
@app.shell_context_processor
def make_shell_context():
return dict(db=db,User=User,Role=Role)
@app.cli.command()
@click.argument('test_names',nargs=-1)
def test(test_names):
"""Run the unit tests."""
import unittest
if test_names:
tests = unittest.TestLoader().loadTestsFromNames(test_names)
else:
tests = unittest.TestLoader().discover('tests')
unittest.TextTestRunner(verbosity=2).run(tests)