我是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,我不知道如何将装饰器分别应用到每个函数。