教程鏈接:http://flask.pocoo.org/docs/0.11/tutorial/dbinit/#tutorial-dbinit無法初始化燒瓶initdb的(瓶教程步驟4)
我下面的教程瓶。這是我的python腳本的當前設置。在本教程的最後,我試圖初始化數據庫。但由於某種原因,我一直在得到同樣的錯誤。
# all the imports
import os
import sqlite3
from flask import Flask, request, session, g, redirect, url_for, abort, \
render_template, flash
# create our little application :)
app = Flask(__name__)
app.config.from_object(__name__)
# Load default config and override config from an environment variable
app.config.update(dict(
DATABASE=os.path.join(app.root_path, 'flaskr.db'),
SECRET_KEY='development key',
USERNAME='admin',
PASSWORD='default'
))
app.config.from_envvar('FLASKR_SETTINGS', silent=True)
def connect_db():
"""Connects to the specific database."""
rv = sqlite3.connect(app.config['DATABASE'])
rv.row_factory = sqlite3.Row
return rv
def init_db():
db = get_db()
with app.open_resource('schema.sql', mode='r') as f:
db.cursor().executescript(f.read())
db.commit()
@app.cli.command('initdb')
def initdb_command():
"""Initializes the database."""
init_db()
print 'Initialized the database.'
def get_db():
"""Opens a new database connection if there is none yet for the
current application context.
"""
if not hasattr(g, 'sqlite_db'):
g.sqlite_db = connect_db()
return g.sqlite_db
@app.teardown_appcontext
def close_db(error):
"""Closes the database again at the end of the request."""
if hasattr(g, 'sqlite_db'):
g.sqlite_db.close()
這是我的命令的輸入:
flask initdb
這是輸出:
Usage: flask [OPTIONS] COMMAND [ARGS]...
Error: No such command "initdb"
您是否按照[此處](http://flask.pocoo.org/docs/0.11/cli/#basic-usage)所述導出了燒瓶應用程序? –
是的,我在這一步做了:http://flask.pocoo.org/docs/0.11/tutorial/setup/#tutorial-setup – YAL
嘗試使用'export FLASK_APP = flaskr/flaskr.py'然後'flask initdb ' – tymbark