[学习记录]flask初步

先放上官方文档

 

起步:

1 from flask import Flask
2 app = Flask(__name__)
3 
4 @app.route('/')
5 def hello_world():
6     return 'Hello World!'
7 
8 if __name__ == '__main__':
9     app.run()

这就完成了最简单的flask应用

 

@app.route为路由,当你访问该路径时,flask会启动下方的函数

如果app.run(host='0.0.0.0')意味着监听公网ip

 

路由中可以添加变量例如

1 @app.route('/user/<username>/<password>')
2 def show_user_profile(username,password):
3     # show the user profile for that user
4     return 'User %s' % username
5 
6 @app.route('/post/<int:post_id>')
7 def show_post(post_id):
8     # show the post with the given id, the id is an integer
9     return 'Post %d' % post_id

如果是字符串则不用添加转换器,转换器支持int,float,path。可以携带多个变量

 

可以在装饰器中添加http方法

@app.route('/login',methods=['GET,'POST'])

 

至此已经完成了基本的flask搭建,可以用来向前端返回数据了。

转载于:https://www.cnblogs.com/trickofjoker/p/11041862.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值