This is the 26th day of my participation in the August Text Challenge.More challenges in August

Flask Blueprint Introduction

Blueprints are a class provided by Flask that provides many of the functions of the Core Objects in Flask, the most important of which is registering routes. Through the Flask blueprints, we can divide the whole project into different modules and add different functions to each module. Blueprints can be shared across an application or across multiple projects. Using blueprints simplifies the development of large projects and extends Flask’s centralized mechanism for registering services in applications.

Application scenarios of blueprints

Decompose an application into a collection of blueprints. This is ideal for large applications. A project can instantiate an application object, initialize several extensions, and register a collection of blueprints.

Register a blueprint with the app with the URL prefix and/or subdomain name. The parameters in the URL prefix/subdomain become the common view parameters (by default) for all view functions under the blueprint, registering a blueprint multiple times in an application with different URL rules.

Provide template filters, static files, templates, and other functionality through blueprints. A blueprint does not have to implement applications or view functions.

When you initialize a Flask extension, you register a blueprint in these cases.

Create a blueprint

In the Flask, I will register the blueprints directly. Note: The blueprints need to be registered in main, otherwise an error will be reported.

from flask import Flask, render_template, url_for, flash
from flask import request,session,g,redirect
from flask_sqlalchemy import SQLAlchemy
import pymysql

app = Flask(__name__,static_url_path='/')
app.config['SECRET_KEY'] = "dsadsaffds"  Set session ID to generate
app.config['SQLALCHEMY_DATABASE_URI'] = 'mysql+pymysql://root:123456@localhost:3306/'
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
db = SQLAlchemy(app)  Instantiate the object


if __name__ == '__main__':
    from Control import commic_control,comment_control,user_control,admin_control
    app.register_blueprint(comment_control.comment)

    app.run(debug=True,host='127.0.0.1',port=3399)
Copy the code

Next comes the actual blueprint creation, which looks like this:

from flask import Blueprint,render_template,url_for,session,request,redirect
comment = Blueprint('comment',__name__,url_prefix="/comment")  The first parameter refers to the blueprint object name (which may be used in the Jinja template).
Copy the code

This completes the creation and registration of blueprints. There are two parameters that need to be introduced:

  • "comment": The name of the blueprint, which is a required parameter, in this case the blueprint name is the constituent view functionendpointPart of.
  • url_prefix="/comment": Indicates the prefix of all routes in the blueprint. For example, the prefix defined here is/commentSo if there is one under this blueprint/helloRoute, so its full URL looks like this127.0.0.1:5000 / comment/helloThe important thing to note here is thaturl_prefixIs not a required parameter.

Disadvantages of blueprints:

You cannot unregister a blueprint after an application is created without destroying the entire application object.

There are three steps to using a blueprint:

Create a blueprint object

blue = Blueprint("blue",__name__)
Copy the code

Perform operations on this blueprint object, such as registering routes, specifying static folders, registering template filters…

@blue.route('/')
def blue_index() :
    return "Welcome to my blueprint"
Copy the code

Register the blueprint object on the application object

app.register_blueprint(blue,url_prefix="/blue")
Copy the code