一种编写 Flask View 的方式

第一次尝试使用这种方式,感觉会很爽!

class UserView(BaseMethodView):
    blueprint = bp
    endpoint = 'users'
    url_rules = [
        (['/users/'],
         {'defaults': {'oid': None}, 'methods': ['GET']}),
        (['/users/'],
         {'methods': ['POST']}),
        (['/users/<int:oid>'],
         {'methods': ['GET', 'PUT', 'DELETE']}),
    ]
    Modal = User

    def post(self):
        return ('ok', 201)

    def put(self, oid):
        return 'PUT:{}'.format(oid)

    def delete(self, oid):
        return 'DELETE:{}'.format(oid)
5 comments