The flask project Blueprint

When using the flask write a project, there may be many modules, not the logic of the code are placed in app.py

With a blueprint can solve this problem. After a blueprint to create a route which does not write immediately, but to register on the app before they can become available routing interfaces. register_blueprint ().

1. first create a blueprint:

from flask import Blueprint

user
= Blueprint("user", __name__) @user.route("/test", methods=['POST', 'GET']) def login(): return "ok"

Parameter Description:

  • The first parameter is the name of a blueprint, a blueprint when the route will be recorded in the registration of the name of each blueprint, if there is a recurring name will throw an exception.
  • The second parameter is the name of the module, this is the best __name__to keep this on the line, the blueprint will record the name of the module

2. Sign up to then blueprint app:

from flask import Flask
from .user.views import user

app
= Flask(__name__)
app.register_blueprint(user, url_prefix='/user')

About this user's logic code can be written down user.views files, plus access the ip address / user can

 

Guess you like

Origin www.cnblogs.com/Strangers/p/12612238.html