This code must be placed in the __init__.py as per the documentation: http://flask.pocoo.org/docs/1.0/tutorial/database/ (Section: Register with the Application)
flaskr/__init__.py
def create_app():
app = ...
# existing code omitted
from . import db
db.init_app(app)
return app
This is the __init__.py code after copying that code
import os
from flask import Flask
def create_app(test_config=None):
# create and configure the app
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:
# load the instance config, if it exists, when not testing
app.config.from_pyfile('config.py', silent=True)
else:
# load the test config if passed in
app.config.from_mapping(test_config)
# ensure the instance folder exists
try:
os.makedirs(app.instance_path)
except OSError:
pass
# a simple page that says hello
@app.route('/hello')
def hello():
return 'Hello, World!'
#database
from . import db
db.init_app(app)
return app
Expected output:
Run the init-db command:
flask init-db
Initialized the database.
There will now be a flaskr.sqlite file in the instance folder in your project.
I'm getting an error while executing the above command:
flask init-db
Usage: flask [OPTIONS] COMMAND [ARGS]...
Error: No such command "init-db".
I even tried copying the exact code from the repo https://github.com/pallets/flask/blob/746b91dfced525651c9b71a3bd075a78d2b9262f/examples/tutorial/flaskr/__init__.py
it is not working even though, same error.
Pls help me with this I'm new to flask and i'm stuck here. cannot proceed further in the documentation unless this code executes
It seems like the command init-db wasn't loaded, check if the snippet for the command creation exists in your db.py.
And also check that you set the FLASK_APPenvironment variable.
I know the tutorial works as written, so please carefully review that you followed all the steps.
Please use Stack Overflow for questions about your own code. This tracker is for issues related to the project itself. Be sure to include a minimal, complete, and verifiable example.
@whippiii
import sqlite3
import click
from flask import current_app, g
from flask.cli import with_appcontext
def inti_app(app):
app.teardown_appcontext(close_db)
app.cli.add_command(init_db_command)
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_appcontext
def init_db_command():
init_db()
click.echo('Initialized the database.')
def get_db():
if 'db' not in g:
g.db = sqlite3.connect(
current_app.congig['DATABASE'],
detect_types=sqlite3.PARSE_DECLTYPES
)
g.db.row_factory = sqlite3.Row
return g.db
def close_db(e=None):
db = g.pip('db', None)
if db is not None:
db.close()
looks like the problem might be a typo:
def inti_app(app):
instead of "init_app".
Does that fix the issue?
make sure you run it from the project directory and not the flaskr directory - so one directory up from flaskr.
@davidism It would be nice if the tutorial was updated to reflect @dtamez comment. Because this was exactly my experience today. I followed the tutorial and it didn't work. I found myself here and I got unblocked. Tested in Windows.
Assuming you're following the tutorial exactly, at no point does it tell you to cd into the package, only to create the directory and remain in the project directory. If you have a better suggestion for the tutorial, I'm open to reviewing a PR.
Most helpful comment
make sure you run it from the project directory and not the flaskr directory - so one directory up from flaskr.