Flask blueprint for routing distribution

Flask blueprint for routing distribution

Although Flask is a lightweight web framework, it can't always write a complete view with a py file, so we need to divide the route into different py files. This requires the use of blueprints.

Create a py file

Used to process the divided url, such as creatingmusic.py

from flask import Blueprint

music = Blueprint('music', __name__)


@music.route("/")	# 即 /music/
def roo():
    return "music"

Second creationmanage.py

manage.pyIt's actually the original app.py, just changed the name

from flask import Flask
from music import music

app = Flask(__name__)
# 注册蓝图,并指定其对应的前缀(url_prefix)
app.register_blueprint(music, url_prefix="/music")


@app.route('/')
def hello_world():
    return 'Hello World!'


if __name__ == '__main__':
    app.run(host="127.0.0.1", port=5000, debug=True)

Three complete

Enter http://127.0.0.1:5000/music/ and
found that "music" is returned, indicating that it has been completed. You can process other routes in music.py in the future

My github
my blog
my notes

Guess you like

Origin www.cnblogs.com/lczmx/p/12682886.html