0

Is there a way to catch common routes to prevent repetitions of decorators(and maybe url parameters)?

My current code structure:

@app.route("/user")
@login_required

@app.route("/user/logout")
@login_required

@app.route("/user/profile")
@login_required

@app.route("/user/profile/settings")
@login_required

I want it to become something like:

@app.route("/user")
@login required

if ("/logout"):
    return template 

elif ("/profile"):
    return template

elif ("/profile/settings"):
    return template

And maybe possible to nest like:

@app.route("/user")
@login required

if ("/logout"):
    if ("/"):
        return template

elif ("/profile"):
    if ("/"):
        return template
    elif ("/settings"):
        return template
roocs
  • 43
  • 1
  • 9

1 Answers1

0

It is not possible in the way you have mentioned but you can use blueprints to make your code more clean and readable. Watch this video by Corey MS where he explains this in very detailed and precise manner. https://m.youtube.com/watch?v=Wfx4YBzg16s&list=PL-osiE80TeTs4UjLw5MM6OjgkjFeUxCYH&index=12&t=0s

Or refers to the official docs for blueprints: https://flask.palletsprojects.com/en/1.1.x/blueprints/

Aditya
  • 184
  • 1
  • 12