如何使用烧瓶发送多个参数到路由?

How to send multiple parameters to route using flask?

最近开始学习Flask框架,做了一个小程序来理解request/response flask中的循环。

我的问题是最后一个方法 calc 不起作用。

我发送请求为:

http://127.0.0.1/math/calculate/7/6

我得到错误:

"Not Found: The requested URL was not found on the server. If you entered the URL manually please check your spelling and try again."

下面是我的烧瓶应用程序代码:

from flask import Flask
app = Flask(__name__)

@app.route('/')
def index():
    return "<h1>Hello, World!</h1>"

@app.route('/user/<name>')
def user(name):
    return '<h1>Hello, {0}!</h1>'.format(name)

@app.route('/math/calculate/<string:var1>/<int:var2>')
def calc(var1, var2):
    return  '<h1>Result: {0}!</h1>'.format(int(var1)+int(var2))

if __name__ == '__main__':
      app.run(host='0.0.0.0', port=80, debug=True)

要访问评论中所述的请求参数,您可以使用请求库:

from flask import request

@app.route('/math/calculate/')
def calc():
    var1 = request.args.get('var1',1,type=int)
    var2 = request.args.get('var2',1,type=int)
    return '<h1>Result: %s</h1>' % str(var1+var2)

此方法的文档记录在此处:

http://flask.pocoo.org/docs/1.0/api/#flask.Request.args

从request.args中提取key值的get方法原型为:

get(key, default=none, type=none)