The place to initialize the database in the flask

I am developing an application in Flask and requires DB, so I have:

app = Flask(__name__) @app.before_request def init_db_connection: # here I connect to my DB @app.teardown_request def destroy_db(exception): # here I destroy database connection 

On the development server (app.run ()) this is not the best place to initialize the database, I think, because the database will also be initialized even when the request comes for a static file. In production, I can have a separate web server serving static files, so this should not be a problem.

But still, I think this is the right way to initialize the database, or is it better, for example, to initialize the database in Blueprint, which is used at this moment? I just want to know the best practices and how you guys are :)

Thanks!

+7
source share
1 answer

The way I did this in the past, and the way this is reflected in the documentation for the flask. I would stick to that.

https://web.archive.org/web/20120825162413/http://flask.pocoo.org/docs/tutorial/dbcon/

+1
source

Source: https://habr.com/ru/post/923784/


All Articles