Home > OS >  When working with blueprints, why do decorators required brackets?
When working with blueprints, why do decorators required brackets?

Time:07-01

I have a blueprint app and I'm trying to integrate Flask Security Too. In my blueprint I have to 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?

CodePudding user response:

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

  • Related