为什么我的添加功能不起作用?
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.
答案 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)