我想使用“flask”命令从终端运行一些命令,但它不起作用。
以下是我的项目结构-
FlaskUserAuthentication
├── FlaskUserAuthentication
│ ├── API
│ │ ├── __init__.py
│ │ ├── db_models.py
│ │ └── routes.py
│ ├── Site
│ │ ├── __init__.py
│ │ ├── routes.py
│ │ ├── static
│ │ │ └── form_style.css
│ │ └── templates
│ │ └── Site
│ │ ├── base_layout.html
│ │ ├── index.html
│ │ ├── logout.html
│ │ ├── profile.html
│ │ ├── signin.html
│ │ └── signup.html
│ ├── __init__.py
│ └── commands.py
├── run.py
└── venv
当flask run
命令运行应用程序时,我确信我的环境变量设置正确。但是,当我尝试使用 flask-cli-command 时-
flask create_db
我明白了,Error: No such command "create_db".
以下是我的FlaskUserAuthentication/commands.py
文件-
from FlaskUserAuthentication import app, db
from FlaskUserAuthentication.API.db_models import Group, Member, Project, Milestone
@app.cli.command('create_db')
def createDatabase():
db.create_all()
print('***** Datebase created ****')
#....some more commands
和FlaskUserAuthentication/__init__.py
模块(启动 Flask 应用程序实例的地方)-
from flask import Flask
from flask_sqlalchemy import SQLAlchemy
app = Flask(__name__)
app.config['SECRET_KEY'] = 'justasamplekey'
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///database.db'
db = SQLAlchemy(app)
from FlaskUserAuthentication.API.routes import api
from FlaskUserAuthentication.Site.routes import site
app.register_blueprint(api)
app.register_blueprint(site)