preface
Select the Flask framework when doing Python Web development. The project module is divided into phases using Blueprint(I’ll call it a “Blueprint” here). Blueprint splits a large application into modules that perform different functions by separating them. A Blueprint view function can be called from one Blueprint with the corresponding Blueprint name.
Blueprint also has other benefits, essentially making programs more loosely coupled and flexible, increasing reusability, improving error detection, and reducing error probability.
In the process of specific project development, different blueprints correspond to different functional modules. Examples are auth authorization module and project master module.
Instance to explain
The actual project development is taken as an example. The project structure is shown in the figure below:
Different blueprints are in different Python packages, and blueprints are created in the __init__.py file in the Python package. The views.py files in different Python packages correspond to routes in different templates.
In the sample project, the blueprint creation code under the Auth license package is as follows:
from flask import Blueprint
auth = Blueprint('auth', __name__)
from . import forms, views
Copy the code
The above blueprint creation statement creates a blueprint named “auth”. The reason why from. import forms and views guide statements are placed at the end of __init__.py is to avoid circular import dependencies because the template auth is also imported in views.py.
The views. py file contains the following contents:
from flask import request, flash, render_template, redirect, Url_for from.import auth from web_flask.app.dal_pymysql import DataManager # admin @auth.route('/login', methods=['POST']) def login(): Form = request.form uname = request.form['username'] PWD = request.form['password']Copy the code
Import the required template here using from. import auth. The template registration statement is placed in the factory method of project creation with the following registration code:
From. Auth import auth as auth_blueprint from. Main import main as main_blueprint app.register_blueprint(auth_blueprint,) app.register_blueprint(main_blueprint, static_folder='static') return appCopy the code
In the blueprint registration function register_blueprint(), the first argument is the name of the registered blueprint. When we register a blueprint on the application object, we need to specify an url_prefix keyword argument (the default is /). The remaining parameters are detailed in Flask official documentation.
Matters needing attention
Note: There are two main differences in writing views.py in the blueprint:
- First, as with the previous routing program, the route decorator is provided by the blueprint;
- Second, the url_for() function is used differently.
As you may recall, the first argument to the url_for() function is the endpoint name of the route, which in the program route defaults to the view function name. For example, in a single-script program, the URL of the index() view function can be obtained using url_for(‘index’).
Flask adds a namespace to all the endpoints in the blueprint, so you can define view functions in different schemas using the same endpoint name without conflict. The namespace is the name of the Blueprint (the first argument to the Blueprint constructor), so the view function index() registers an endpoint name as main.index, whose URL is obtained using url_for(‘main.index’).
The url_for() function also supports a shorthand endpoint form in which the blueprint name can be omitted, such as url_for(‘.index’). In this writing, the namespace is the blueprint for where the current request is located. This means that redirects within the same blueprint can be abbreviated, but redirects across the blueprint must use endpoint names with namespaces.
If you do not use url_for(‘main.index’) or url_for(‘.index’), the requested URL error will be prompted during the project run.