Bootstrap

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

;