BlueprintFlask framework is one of the way to write multi-module applications, using the flask framework for writing projects, there may be many modules, such as the front and back-end mobile version… If all modules are written in a.Py file, the file will be veryThe bloated, very difficult to maintain, but also not beautiful. So the flask framework introduces the concept of Blueprint (blueprint). A.Py file can be separated into multiple modules.
After the module is separated, the structure is
In this file structure, manage.py is the entry file, the package admin is the background, the package home is the foreground, and the views.py file is the view
The business is written to define the Blueprint in each module.
adminModule __inti__.py
from flask import Blueprint admin=Blueprint('admin',__name__) #Defining blueprint objectsImport app.admin.views
View views.py
from . import admin @admin.route('/') def index(): return 'Backstage here
homeModule __init__.py
from flask import Blueprint home=Blueprint('home',__name__) import app.home.views
View views.py
from . import home @home.route('/') def index(): return 'I'm the front desk.
Configuration file __init__.py under project app
from flask import Flask app=Flask(__name__) #app.debug=True from app.home import home as home_blueprint #Introducing blueprint objectsFrom app.admin import admin as admin_blueprintApp.register_blueprint (home_blueprint)App.reGister_blueprint (admin_blueprint, url_prefix='/admin')
Entry file manage.py
from app import app if __name__ == '__main__': app.run(debug=True)
Running entry file
Summary: