CoreyMSchafer/code_snippets

Does it the right way to create database for flask blueprint?

Opened this issue · 1 comments

when learning : https://github.com/CoreyMSchafer/code_snippets/tree/master/Python/Flask_Blog/11-Blueprints

when I tried rebuilding it and creating database use former methods, I get an error:```
... 'No application found. Either work inside a view function or push'
...


after a few research, I tried to solve it with adding following function in __init__.py:

def create_db():
    app = create_app()
    with app.app_context():
        db.create_all()

and run it in standalone python shell after importing:  from flask_blog import *

and it worked.

**But,** I wondering is it the _right_ way? 

Thanks, and very nice tutorials.

You can get also around this error without adding the function to init.py by doing:

$ python
>>> from flask_blog import create_app
>>> app = create_app()
>>> app.app_context().push()
>>> from flask_blog import db
>>> db.create_all()