gpt4 book ai didi

python - flask 应用程序 : The requested URL was not found on the server

转载 作者:太空宇宙 更新时间:2023-11-03 16:29:32 32 4
gpt4 key购买 nike

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

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.

最佳答案

将添加方法更改为以下内容,现在它可以工作了:

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

更新:进一步说明,如果您不想在输入 float 时收到 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)

关于python - flask 应用程序 : The requested URL was not found on the server,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37719685/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com