Flask app:在服务器上找不到请求的URL

时间:2016-06-09 07:38:31

标签: python python-3.x flask

为什么我的添加功能不起作用?

from flask import Flask
from flask import request
app = Flask(__name__)
@app.route('/')
@app.route('/<name>')
def index(name="Mona"):
    #name= request.args.get('name', name)
    return "Hello from {}".format(name)
@app.route('/add/num1/num2')
def add(num1, num2):
    return '{} + {} = {}'.format(num1, num2, num1 + num2)
app.run(debug=True, port=8002, host='0.0.0.0')

浏览0.0.0.0:8002/add/1/4

时出现此错误
Not Found

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

1 个答案:

答案 0 :(得分:1)

将add方法更改为以下内容,现在它正在运行:

@app.route('/add/<int:num1>/<int:num2>')
def add(num1, num2):
    return '{} + {} = {}'.format(num1, num2, num1 + num2)

更新:进一步说明,如果您想在输入浮动时不收到404错误,请输入以下代码:

@app.route('/add/<int:num1>/<int:num2>')
@app.route('/add/<int:num1>/<float:num2>')
@app.route('/add/<float:num1>/<int:num2>')
@app.route('/add/<float:num1>/<float:num2>')
def add(num1, num2):
    return '{} + {} = {}'.format(num1, num2, num1 + num2)