flask 蓝图简单格式(一)

使用蓝图的方便之处就是 将不同的地址分离,不至于臃肿,便于后续功能扩展

manage.py

 1 from apps.test1  import test1 #地址一
 2 from apps.test2  import test2 #地址二
 3 
 4 app = Flask(__name__)
 5 app.register_blueprint(test1) 
 6 app.register_blueprint(test2) 
 7 
 8 
 9 @app.route('/index')
10 def index():
11     return render_template("index.html")
12 
13 @app.errorhandler(404)
14 def not_found(e):
15     return render_template("404.html")
16 
17 if __name__ == '__main__':
18     app.run(host='0.0.0.0',port=5000,debug=true)
View Code

apps.test1

from flask import Blueprint, render_template

test1= Blueprint('test1', __name__)

@test1.route('/xxxxx',methods=['GET','POST'])
def xxxx():
    return '11'

`
`
`
`
`
View Code

apps.test2 

 1 from flask import Blueprint, render_template
 2 
 3 test2= Blueprint('test2', __name__)
 4 
 5 @test2.route('/xxxxx',methods=['GET','POST'])
 6 def xxxx():
 7     return '11'
 8 
 9 `
10 `
11 `
12 `
13 `
View Code

注:

此种格式没有用到 前缀,访问 直接访问,不需要加 test1 、test2  

如果需要访问需要前缀,可以使用  

1 app.register_blueprint(test1, url_prefix='/test1')
url_prefix

猜你喜欢

转载自www.cnblogs.com/whycai/p/12650459.html