Flask study notes (5) - the method used by the Flask blueprint

We have learned some basic usages of Flask through code before, but now there is a problem, we have to do more and more functions, should the routing be placed in the startup class? For example, we have defined some routes in different files. If we want to access them, do we need to open many different services?

Blueprints are provided in Flask, specifically for Flask's modularization. For the blueprint, you can see the official introduction, which is translated here:

Flask uses the concept of blueprints to make application components and support common patterns within or across applications. Blueprints can greatly simplify how large applications work, and provide a central means for Flask extensions to register operations on the application. The Blueprint object works similarly to the Flask application object, but it's not actually an application. Rather, it is a blueprint for how to structure or extend an application.

In short, the blueprint can make our program more modular, and the routes of different functions can be placed in different modules, and finally concentrated in the startup class. Let's take a look at an example to learn to use blueprints, based on the previous code, with a few modifications.

Sub-routing class:

from flask import Flask, render_template, Blueprint

app = Flask(__name__)
test1 = Blueprint('test1', __name__)
@test1.route('/test')
def test():
    return render_template('test1.html')


Sub-routing class:

#encoding:utf-8
#!/usr/bin/env python
from flask import Flask, render_template, request, redirect, url_for, session, flash, Blueprint
import sys
reload(sys)
sys.setdefaultencoding('utf8')
app = Flask(__name__)
form_test = Blueprint('form_test', __name__)

app.config['SECRET_KEY'] = 'my'
app.secret_key = 'my'

@form_test.route('/index')
def index():
    return render_template('test1.html')

@form_test.route('/login', methods=['get', 'post'])
def login():
    name = request.form.get('name')
    password = request.form.get('password')
    if name == 'admin' and password == '123':
        session['name'] = name
        return redirect(url_for('login'))
    if name != 'admin':
        flash('no this name')
    return render_template('test1.html', name=session.get('name'))


Main route startup class:

#encoding:utf-8
#!/usr/bin/env python
from flask import Flask, render_template
from test1 import test1
from form_test import form_test
from flask_script import Manager
app = Flask(__name__)
app.register_blueprint(form_test)
app.register_blueprint(test1)

@app.route('/')
def index():
    num = ['tom', 'mike', 'amuxia', 'zhao', 'lisi']
    return render_template('/index.html', num=num)

@app.errorhandler(404)
def miss(e):
    return render_template('404.html'), 404

@app.errorhandler(500)
def error(e):
    return render_template('500.html'), 500

if __name__=='__main__':
    print(app.url_map)
    app.run()

Looking at the above code, you should also understand that the blueprint is created by form_test = Blueprint('form_test', __name__) in the sub-route, and then injected through app.register_blueprint(form_test) in the main route.


Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324601680&siteId=291194637