This section describes the initial settings when you start Flask.
__init__.pyThe __init__.py is as follows, although it varies slightly depending on the database and application used.
__init__.py
import os
from flask import Flask
from . import db, auth, views
def create_app(test_config=None):
    app = Flask(__name__, instance_relative_config=True)
    app.config.from_mapping(
        SECRET_KEY = 'dev', #For development
        DATABASE = os.path.join(app.instance_path, 'Project name.sqlite'),
    )
    if test_config is None:
        app.config.from_pyfile('config.py', silent=True)
    else:
        app.config.from_mapping(test_config)
    try:
        os.makedirs(app.instance_path)
    except OSError:
        pass
    
    db.init_app(app)
    app.register_blueprint(auth.bp)
    app.register_blueprint(views.bp)
    app.add_url_rule('/', endpoint='index')
    return app
The SECRET KEY is only set for development for the time being and needs to be changed during deployment.
Also, ʻinit_app is a method for initializing the application. I will explain it in a later article.  ʻApp.register_blueprint describes ʻauth and views as an example here, but it is necessary to describe each application that sets blueprint.  In ʻadd_url_rule, the URL of the index page is specified in this example.
Here, I explained the initial settings when starting a Flask project. Database and application settings will be covered at a later date.
Recommended Posts