首页 > 其他 > 详细

flask 蓝图简单格式(一)

时间:2020-04-07 01:16:02      阅读:59      评论:0      收藏:0      [点我收藏+]

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

 

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

flask 蓝图简单格式(一)

原文:https://www.cnblogs.com/whycai/p/12650459.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!