Gau*_*hah 2 python rest flask flask-restful
我试图理解Flask-RESTful,但是我不知道如何为资源提供可选参数。
即:
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>')
Run Code Online (Sandbox Code Playgroud)
在上述情况下,如何创建一个新的端点,该端点将返回所有待办事项,而不仅仅是一个?
摘自:https : //flask-restful.readthedocs.io/en/0.3.5/quickstart.html
我认为最好的方法是拥有两个资源/端点。第一个用于管理集合(获取待办事项列表,添加新的待办事项),第二个用于管理集合的项目(更新或删除项目):
class TodoListResource(Resource):
def get(self):
return {'todos': todos}
class TodoResource(Resource):
def get(self, todo_id):
return {todo: todos[todo_id]}
def put(self, todo_id):
todos[todo_id] = request.form['data']
return {todo: todos[todo_id]}
api.add_resource(TodoListResource, '/todos')
api.add_resource(TodoResource, '/todos/<string:todo_id>/')
Run Code Online (Sandbox Code Playgroud)
这种方式是更多的REST。
| 归档时间: |
|
| 查看次数: |
711 次 |
| 最近记录: |