__init__.py 文件
# -*- coding = utf-8 -*-
import os
from flask import Flask
def create_app(test_config=None):
app = Flask(__name__, instance_relative_config=True)
app.config.from_mapping(
SECRET_KEY='dev',
DATABASE=os.path.join(app.instance_path, 'flaskr.sqlite')
)
if test_config is None:
app.config.from_pyfile('
config.py', silent=True)
else:
app.config.from_mapping(test_config)
try:
os.makedirs(app.instance_path)
except OSError:
pass
@
app.route('/hello')
def hello():
return 'Hello, World!'
return app
from . import db
db.init_app(app)
return app
db.py 文件
# -*- coding=utf-8 -*-
import sqlite3
import click
from flask import current_app, g
from flask.cli import with_appcontext
def get_db():
if 'db' not in g:
g.db = sqlite3.connect(
current_app.config['DATABASE'],
detect_types=sqlite3.PARSE_DECLTYPES
)
g.db.row_factory = sqlite3.Row
return g.db
def init_db():
db = get_db()
with current_app.open_resource('schema.sql') as f:
db.executescript(f.read().decode('utf8'))
@
click.command('init-db')
@
with_appcontextdef init_db_command():
init_db()
click.echo('Initialized the database.')
def close_db(e=None):
db = g.pop('db', None)
if db is not None:
db.close()
def init_app(app):
app.teardown_appcontext(close_db)
app.cli.add_command(init_db_command)
目录结构:
flaskr/
__init__.py db.py schema.sql
FLASK_APP, FLASK_ENV 都已经设置,flask run 运行是正常的,无报错。执行 flask init-db 报错提示找不到这个命令:Error: No such command "init-db". 请问各位大神是什么原因啊,我网上的方法都试了解决不呢?