0

I am trying to split up my routing addresses into separate routing calls but I don't know if it's possible with flask? Does this approach work?

@app.route("/auth")
    @app.route("/login")
        def login():
            #do login stuff
            return render_template()

    @app.route("/logout")
        def logout():
            #do logout stuff
            return render_template()
    @app.route("/register")
        def register():
            #do register stuff
            return render_template()

if I went to address /auth/login, I would expect the routing to first take me to api.route("/auth") and then api.route("/login")

A.Gros
  • 13
  • 2
  • You can write a wrapper function `auth` and use it as a decorator. This may help: https://stackoverflow.com/questions/19797709/what-is-a-self-written-decorator-like-login-required-actually-doing – amanb Apr 03 '19 at 14:08

1 Answers1

0

There's a couple of ways to accomplish this.

For large projects, consider using blueprints. A great introduction can be found here

Otherwise your code will have to look like either of the following.

@app.route("/auth/login")
def login():
    #do login stuff
    return render_template()

@app.route("/auth/logout")
def logout():
    #do logout stuff
    return render_template()

@app.route("/auth/register")
def register():
    #do register stuff
    return render_template()

or

from flask import abort

@app.route("/auth/<destination>")
def auth(destination):
    if destination == "login":
        #do login stuff
        return render_template()
    elif destination == "logout":
        #do logout stuff
        return render_template()
    elif destination == "register":
        #do register stuff
        return render_template()
    else:
        return abort(404)
Brannon
  • 116
  • 5