我在这里找到烧瓶教程:http://flask.pocoo.org/docs/0.12/tutorial/。我已经按照步骤0-4的步骤操作,但无法理解步骤5(创建数据库);我已阅读并增加了培训相关功能,以我的flaskr.py脚本,所以它目前看起来是这样的:Python烧瓶:无法初始化SQLite数据库
# all the imports
import os
import sqlite3
from flask import Flask, request, session, g, redirect, url_for, abort, render_template, flash
app = Flask(__name__) # create the application instance :)
app.config.from_object(__name__) # load config from this file , flaskr.py
# 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 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()
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.')
if __name__ == '__main__':
init_db()
app.run()
本教程接着说,该数据库可以通过调用初始化:
flask initdb
运行此命令产量:
Usage: flask [OPTIONS] COMMAND [ARGS]...
Error: No such command "initdb".
从迄今为止以下的教程中,我没有在我的应用程序目录中flask
脚本。我还做了一些研究,发现这个资源:http://flask.pocoo.org/docs/0.12/cli/,其中指出这个烧瓶脚本带有虚拟环境,但是我的虚拟环境没有这个文件。我已通过导航到我的根目录并使用以下方法进行验证:
find . -name flask
find . -name flask.py
但是,这些命令都不会返回任何匹配。由于我对Flask比较新,所以我可能会在这里错过一些简单的东西;有人可以解释我缺少的东西并提供解决方法吗?提前致谢!
只是可以肯定,你可以在你的问题包括'画中画显示Flask'输出?在你的virtualenv(当然)中做。 – skytreader
@skytreader:'pip show烧瓶'产量:'名称:烧瓶 版本:0.12 摘要:基于Werkzeug,Jinja2和好心的微框架 主页:http://github.com/pallets/flask/ 作者:Armin Ronacher 作者-email:[email protected]。com 许可证:BSD 位置:/home/neil/Desktop/flaskr/lib/python2.7/site-packages 需要:itsdangerous,click,Werkzeug,Jinja2' –