Python decorator with Flask

前端 未结 2 1842
终归单人心
终归单人心 2021-01-12 04:37

I need to add a python decorator to Flask route functions, (basically I edited the code from here)

def requires_admin(f):
    def wrapper(f):
        @wraps(         


        
2条回答
  •  暗喜
    暗喜 (楼主)
    2021-01-12 04:56

    Ok I solved this problem by reading this answer Route to view_func with same decorators "flask" given by @will-hart

    I simply remove the def wrapper(f) and everything seems fine now. at leaset no grammar error.

    from functools import wraps
    
    def requires_admin(f):
        @wraps(f)
        def wrapped(*args, **kwargs):
            #if blah blah:
                #return blah blah
            return f(*args, **kwargs)
        return wrapped
    

    Since I am pretty new to decorator and I dont know why. But hope this can help other ppl.

提交回复
热议问题