【发布时间】:2017-12-01 10:02:52
【问题描述】:
我是 Flask 的新手,我的学业需要一些帮助。
我正在尝试使用 flask-restful 构建一个简单的 ToDo 列表系统。
我当前的代码如下所示:
class ToDoList(Resource):
'''TODO LIST'''
operation = ['delete']
decorators = [auth.login_required, advertise('operation')]
def post(self):
"""remove all item in the TODO list"""
operation = request.args.get('op')
if operation == 'delete':
collection2.delete_many({})
return {'Success': 'OK'}, 200
return {'Error':'Illegal Operation'}, 400
def get(self):
"""return a list of the TODO name"""
list_1 = collection2.find()
list_2 = []
for each in list_1:
list_2.append(JSONEncoder().encode(each))
return {'list':list_2}, 200
它有效,但我只希望 post 方法需要身份验证,而 get 方法不需要身份验证,因此任何人都可以在不登录的情况下获取列表。我正在使用flask-restful,我不知道如何将装饰器分别赋予每个函数。
【问题讨论】:
标签: python flask flask-restful