Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I use the same route for multiple functions in Flask

I'm currently using python3 and Flask; I have two functions defined using the same route. - How can I get index2 to print.

from flask import Flask, request, make_response

app = Flask(__name__)

@app.route('/')
def index():
    if request.authorization and request.authorization.username == 'user1' and request.authorization.password == 'pass1':
        return '<h1>You are logged in</h1>'
    return make_response('Could not verify!', 401, {'WWW-Authenticate' : 'Basic realm="Login Required"'})

@app.route('/')
def index2():
    print('In Index 2')

if __name__ == '__main__':
    app.run(debug=True)
like image 524
Afshan Anwarali Avatar asked Oct 15 '18 06:10

Afshan Anwarali


People also ask

Can you use multiple decorators to route URLs to a function in Flask?

We can use multiple decorators by stacking them.

Can we have multiple URL paths pointing to the same route function?

In some cases you can reuse a Flask route function for multiple URLs. Or you want the same page/response available via multiple URLs. In that case you can add a second route to the function by stacking a second route decorator to the function. The code below shows how you use the a function for multiple URLs.

How do you use a Flask routing?

Step to run the application: Run the application using the following command. Output: Open the browser and visit 127.0. 0.1:5000/hello, you will see the following output. Dynamic URLs – We can also build dynamic URLs by using variables in the URL.

How do you add a route in Flask?

Adding routes is quite easy, all we need to do is use the Python decorator again followed by a new function. An 'about' route is shown highlighted here. After adding this code, the development server expectedly notices the change and reloads the server for us.


1 Answers

You have multiple options; one of which is to call index2 function from within the index function:

from flask import Flask, request, make_response

app = Flask(__name__)

@app.route('/')
def index():
    if request.authorization.username == 'user1' and request.authorization.password == 'pass1':
        index2() # you can return index2() if that's the logged in page.
        return '<h1>You are logged in</h1>'

    return make_response('Could not verify!', 401, {'WWW-Authenticate' : 'Basic realm="Login Required"'})


def index2():
    print('In Index2')

if __name__ == '__main__':
    app.run(debug=True)

The second option is to differ both functions based on the http method being called:

from flask import Flask, request, make_response

app = Flask(__name__)

@app.route('/')
def index():
    if request.authorization.username == 'user1' and request.authorization.password == 'pass1':
        return '<h1>You are logged in</h1>'

    return make_response('Could not verify!', 401, {'WWW-Authenticate' : 'Basic realm="Login Required"'})

@app.route('/', methods=['POST'])
def save():
    print('Save operations here')

if __name__ == '__main__':
    app.run(debug=True)

The third option is to use different parameters:

from flask import Flask, request, make_response

app = Flask(__name__)

@app.route('/')
def index():
    if request.authorization.username == 'user1' and request.authorization.password == 'pass1':
        return '<h1>You are logged in</h1>'

    return make_response('Could not verify!', 401, {'WWW-Authenticate' : 'Basic realm="Login Required"'})

@app.route('/<string:page_name>')
def index2(page_name):
    print(f"{page_name}")

if __name__ == '__main__':
    app.run(debug=True)
like image 158
Julian Camilleri Avatar answered Oct 14 '22 18:10

Julian Camilleri