简体   繁体   中英

When working with blueprints, why do decorators required brackets?

I have a blueprint app and I'm trying to integrate Flask-Security-Too . In my blueprint I have two routes:

@core_bp.route("/")
@auth_required
def home():
   print("test")
   return render_template('/core_bp/index.html')


@core_bp.route("/get-projects")
@auth_required
def get_projects():
    searchTerm = request.args.get("search")
    data = [
        "buchsweg 5" ,
        "tannenweg 1"
    ]

return jsonify(data)

In this case I get the following error:

AssertionError: View function mapping is overwriting an existing endpoint function: core_bp.wrapper

But when I replace auth_required with @auth_required() it's working just fine? It's working now, but it would be nice to understand why it's working?

You need the parenthesis because there are 3 optional arguments that can be passed to the decorator as you can see here in line 275

https://github.com/Flask-Middleware/flask-security/blob/master/flask_security/decorators.py

In general some_decorator is a regular decorator, whereas, some_decorator() is a callable that returns a decorator as stated in this former question:

Using python decorator with or without parentheses

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM