Unable to init_db in tutorial: Working outsite of application context #4793
Answered
by
davidism
Wishmaster69200
asked this question in
Q&A
-
I'm following the Flask tutorial https://flask.palletsprojects.com/en/2.2.x/tutorial/database/ but I'm stuck on initializing the database. I have import sqlite3
import click
from flask import current_app
from flask import g
def get_db():
"""Connect to the application's configured database. The connection
is unique for each request and will be reused if this is called
again.
"""
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 close_db(e=None):
"""If this request connected to the database, close the
connection.
"""
db = g.pop("db", None)
if db is not None:
db.close()
def init_db():
"""Clear existing data and create new tables."""
db = get_db()
with current_app.open_resource("schema.sql") as f:
db.executescript(f.read().decode("utf8"))
@click.command("init-db")
def init_db_command():
"""Clear existing data and create new tables."""
init_db()
click.echo("Initialized the database.")
def init_app(app):
"""Register database functions with the Flask app. This is called by
the application factory.
"""
app.teardown_appcontext(close_db)
app.cli.add_command(init_db_command)` When I run Traceback (most recent call last):
File "/home/MBABE47N/flask-tutorial/venv/bin/flask", line 11, in <module>
sys.exit(main())
File "/home/MBABE47N/flask-tutorial/venv/lib64/python3.6/site-packages/flask/cli.py", line 995, in main
cli.main(args=sys.argv[1:])
File "/home/MBABE47N/flask-tutorial/venv/lib64/python3.6/site-packages/flask/cli.py", line 601, in main
return super().main(*args, **kwargs)
File "/home/MBABE47N/flask-tutorial/venv/lib64/python3.6/site-packages/click/core.py", line 1053, in main
rv = self.invoke(ctx)
File "/home/MBABE47N/flask-tutorial/venv/lib64/python3.6/site-packages/click/core.py", line 1659, in invoke
return _process_result(sub_ctx.command.invoke(sub_ctx))
File "/home/MBABE47N/flask-tutorial/venv/lib64/python3.6/site-packages/click/core.py", line 1395, in invoke
return ctx.invoke(self.callback, **ctx.params)
File "/home/MBABE47N/flask-tutorial/venv/lib64/python3.6/site-packages/click/core.py", line 754, in invoke
return __callback(*args, **kwargs)
File "/home/MBABE47N/flask-tutorial/flaskr/db.py", line 42, in init_db_command
init_db()
File "/home/MBABE47N/flask-tutorial/flaskr/db.py", line 33, in init_db
db = get_db()
File "/home/MBABE47N/flask-tutorial/flaskr/db.py", line 13, in get_db
if "db" not in g:
File "/home/MBABE47N/flask-tutorial/venv/lib64/python3.6/site-packages/werkzeug/local.py", line 436, in __get__
obj = instance._get_current_object()
File "/home/MBABE47N/flask-tutorial/venv/lib64/python3.6/site-packages/werkzeug/local.py", line 565, in _get_current_object
return self.__local() # type: ignore
File "/home/MBABE47N/flask-tutorial/venv/lib64/python3.6/site-packages/flask/globals.py", line 40, in _lookup_app_object
raise RuntimeError(_app_ctx_err_msg)
RuntimeError: Working outside of application context.` |
Beta Was this translation helpful? Give feedback.
Answered by
davidism
Aug 30, 2022
Replies: 1 comment 1 reply
-
Upgrade to Flask 2.2. |
Beta Was this translation helpful? Give feedback.
1 reply
Answer selected by
Wishmaster69200
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Upgrade to Flask 2.2.