flask blueprint for understanding

Blueprint

Modular

With the flask procedure more complex, we need a program of modular process, modular python previously studied management, so modular treatment program for a simple flask

for example:

We have a blog program, front-end interface routing is required: Home, lists, details, etc. page

源程序app.py文件:
from flask import Flask

app=Flask(__name__)

@app.route('/')
def index():
    return 'index'

@app.route('/list')
def list():
    return 'list'

@app.route('/detail')
def detail():
    return 'detail'

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

If bloggers need to edit the blog, to be processed into the background: the background Home, edit, create, publish blog

改进后程序:
from flask import Flask

app=Flask(__name__)

@app.route('/')
def index():
    return 'index'

@app.route('/list')
def list():
    return 'list'

@app.route('/detail')
def detail():
    return 'detail'

@app.route('/')
def admin_home():
    return 'admin_home'

@app.route('/new')
def new():
    return 'new'

@app.route('/edit')
def edit():
    return 'edit'

@app.route('/publish')
def publish():
    return 'publish'

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

This allows us to write a lot py file routing, the future maintenance of the code would be very difficult, at this time, the students will take into account the modular approach, the admin wrote a admin.py associated routing file, along the way then we go

修改后的代码:
app.py
from flask import Flask

app=Flask(__name__)

@app.route('/')
def index():
    return 'index'

@app.route('/list')
def list():
    return 'list'

@app.route('/detail')
def detail():
    return 'detail'

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

admin.py

@app.route('/')
def admin_home():
    return 'admin_home'

@app.route('/new')
def new():
    return 'new'

@app.route('/edit')
def edit():
    return 'edit'

@app.route('/publish')
def publish():
    return 'publish'

App.py file found in app direct error, can not continue writing the code, so in the flask procedure, using traditional modular does not work, you need to provide a flask unique modular approach, it built a modular flask handling, i.e. Blueprint

Blueprint concept

Briefly, the Blueprint container is a storage method of operation, these operations can be registered to be called after a application of this Blueprint, Flask may be organized by a processing request and a URL Blueprint.

Flask Blueprint allows applications to use modular, with Flask, Blueprint has the following attributes:

  • An application may have a plurality of Blueprint
  • Blueprint can be registered to a URL in any unused such as "/", "/ sample" or subdomain
  • In one application, a module can be registered multiple times
  • Blueprint can have your own templates alone, static files or other common methods of operation, it is not necessary to achieve the application of views and functions
  • When an application is initialized, it should be registered requires the use of Blueprint

But a Blueprint is not a complete application, it can not be run independent of the application, but must be registered to a particular application.

Acquaintance blueprint

Blueprint / Blueprint with objects together and an application / Flask objects similar, the biggest difference is that the object does not have a blueprint for the way run independently, you must register it to an application object to take effect

Use blueprint can be divided into three steps

  • 1, create a blueprint for an object
admin=Blueprint('admin',__name__)
  • 2, in this blueprint object manipulation, registration routing, specify static folder, registered template filter
@admin.route('/')
def admin_home():
    return 'admin_home'
  • 3, register the blueprint for objects on the application object
app.register_blueprint(admin,url\_prefix='/admin')

When the application starts, by / admin / access to view the function defined blueprint

Operating mechanism

  • Blueprint is saved a set of operations that can be performed in the future registration route on the application object is a kind of operation
  • When you call route decorator registered route on the application object, the object of this operation will modify the routing table url_map
  • However, there is no blueprint for an object routing table, when we call route decorator registered blueprint route on the object, it is only an internal delay in operation records have been added to a list of items defered_functions
  • When the execution of the application object register_blueprint () method, application objects defered_functions list of objects removed from the blueprint each and its own parameters as add_url_rule implementation of the anonymous function that calls the application object () method, which will be true modify the application object routing table

url prefix blueprint

  • When we register a blueprint on the application object, you can specify a url_prefix key parameters (this is the default parameter /)
  • In the application of the final routing table url_map, the blueprint registered on the routing automatically add this URL prefix, this can ensure that instead of using the same URL rule in more than a blueprint for the final conflict, as long as the registration blueprint different blueprint attached to a different path to self

  • url_for

url_for('admin.index') # /admin/

Guess you like

Origin blog.csdn.net/qwertyuiopasdfgg/article/details/93338165