Flask 路由和URL传参数

控制器文件IndexController.py

from flask import Flask;

app = Flask(__name__);
# 不区分URL最后的斜杠
app.strict_slashes = False;

@app.route('/flask')
def hello_flask():
   return 'Hello Flask';

@app.route('/python/')
def hello_python():
    return 'Hello Python';

# URL传参
# 参数格式:<type:variableName>, type默认为string
@app.route('/blog/<int:postID>')
def show_blog(postID):
    return 'Blog Number %d' % postID;

if __name__ == '__main__':
    app.run('0.0.0.0', 8080, debug = True);

运行

$ python IndexController.py
 * Restarting with stat
 * Debugger is active!
 * Debugger PIN: 158-818-550
 * Running on http://0.0.0.0:8080/ (Press CTRL+C to quit)

使用浏览器访问

http://localhost:8080/flask
http://localhost:8080/python
http://localhost:8080/python/

http://127.0.0.1:8080/blog/11

使用curl访问

$ curl http://localhost:8080/flask
$ curl http://127.0.0.1:8080/blog/11

参考:
https://www.tutorialspoint.com/flask/flask_variable_rules.htm

相关文章
简单应用- Hello world

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值