flaskrestful:在上找不到请求的URL

2024-05-08 01:11:31 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图遵循flaskrestful的文档,并尝试运行以下代码。在

from flask import Flask, request
from flask_restful import Resource, Api

app = Flask(__name__)
api = Api(app)

todos = {}

class TodoSimple(Resource):
    def get(self, todo_id):
        return {todo_id: todos[todo_id]}

    def put(self, todo_id):
        todos[todo_id] = request.form['data']
        return {todo_id: todos[todo_id]}

api.add_resource(TodoSimple, '/<string:todo_id>')

if __name__ == '__main__':
    app.run(debug=True)

但是当我试图用“http://127.0.0.1:5000/todo1”URL运行它时,它的响应是消息“请求的URL在服务器上找不到”。如果您手动输入URL,请检查拼写并重试。“。我怎么把代码弄错了。请帮忙。在


Tags: 代码namefromimportapiidappurl
3条回答

问题在于为资源定义url路由的方式。现在您正试图通过http://127.0.0.1:5000/todo1来访问它,但是您已经定义了TodoSimple来服务发送到http://127.0.0.1:5000/1的请求。我建议把代码改成下面这样的代码

api.add_resource(TodoSimple, '/todo/<int:todo_id>')

然后,尝试通过GET http://127.0.0.1:5000/todo/1访问它

相关问题 更多 >