Splitting Flask Routing Functions into Different Modules
We can put group-related routing functions into separate modules to keep a big project organized and avoid having one long views.py in your project. For example, we can take the login and sign-up-related functions from views.py
and move them into a module named auth.py
. In auth.py
, we need to register a new blueprint:
In auth.py
:
from flask import Blueprint # ... dashboard = Blueprint("dashboard", __name__)
In __init__.py
or wherever you have your app = Flask(__name__)
:
from .auth import auth # ... app.register_blueprint(auth, url_prefix="/")
That’s all.