Flask Study Note 3

Views: 290
Wrote on May 15, 2020, 4:02 p.m.


A blueprint is an object that allows defining application functions without requiring an application object ahead of time. It uses the same decorators as Flask, but defers the need for an application by recording them for later registration.

# app.py
from services import users
# /services/users.html
from flask import Blueprint, render_template

users_bpapp = Blueprint("users", __name__, template_folder="users_templates")

@users_bpapp.route("/add_user/", endpoint="add_user")
def add_users():
    return render_template('users.html')

A blueprint is a template for generating a "section" of a web application. You can think of it as a mold. You can take the blueprint and apply it to your application in several places. Each time you apply it the blueprint will create a new version of its structure in the plaster of your application.

# An example
from flask import Blueprint

tree_mold = Blueprint("mold", __name__)

def leaves():
    return "This tree has leaves"

def roots():
    return "And roots as well"

def rings(year=None):
    return "Looking at the rings for {year}".format(year=year)

This is a simple mold for working with trees - it says that any application that deals with trees should provide access to its leaves, its roots, and its rings (by year). By itself, it is a hollow shell - it cannot route, it cannot respond, until it is impressed upon an application:

from tree_workshop import tree_mold

app.register_blueprint(tree_mold, url_prefix="/oak")
app.register_blueprint(tree_mold, url_prefix="/fir")
app.register_blueprint(tree_mold, url_prefix="/ash")

Once it is created it may be "impressed" on the application by using the register_blueprint function - this "impresses" the mold of the blueprint on the application at the locations specified by url_prefix.


similar to the middleware in Django Registers a function to run before each request. For example, this can be used to open a database connection, or to load the logged in user from the session. The function will be called without any arguments. If it returns a non-None value, the value is handled as if it was the return value from the view, and further request handling is stopped.

def is_login():
    if request.path == "/login":
        return None
    if session.get("user"):
        return None
        return redirect("/login")


Register a function to be run after each request.Your function must take one parameter, an instance of response_class and return a new response object or the same (see process_response()).

def is_login():
    if request.path == "/login":
        return None
    if session.get("user"):
        return None
        return redirect("/login")

def be2():
    return None

def be3():
    return None

def af1(res):
    return res

def af2(res):
    return res

def af3(res):
    return res

If there is no error, you'll see:

be1 - be2 - be3 - af3 - af2 - af1 # opposite way how you define the `after_request` decorators.


be1 - af3 - af2 - af1


Register a function to handle errors by code or exception class.

A decorator that is used to register a function given an error code. Example:

def page_not_found(error):
    return 'This page does not exist', 404

The error information is in error arg. You can also register handlers for arbitrary exceptions:

def special_exception_handler(error):
    return 'Database connection failed', 500