I am using flask-login plugin for my application, which is already developed to a good extent.
I want to mark all the url endpoints as "@login_required".
Is there any way I can do this in one place with a single line of code rather than marking every view function with @login_required.
You can define your own decorator. Matt Wright's overholt template provides an example that does exactly what you want.
from functools import wraps
from flask_security import login_required
def route(bp, *args, **kwargs):
def decorator(f):
@bp.route(*args, **kwargs)
@login_required
@wraps(f)
def wrapper(*args, **kwargs):
return f(*args, **kwargs)
return f
return decorator
You can use this decorator to add routes to any endpoints used by the blueprint instead of using the blueprint's route decorator directly.
bp = Blueprint('name', __name__)
@route(bp, '/')
def route1():
return 'Hello, world!'
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With