Afshan Anwarali
Afshan Anwarali

Reputation: 357

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)

Upvotes: 17

Views: 30946

Answers (2)

Julian Camilleri
Julian Camilleri

Reputation: 3105

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)

Upvotes: 22

gittert
gittert

Reputation: 1308

To call index2, try the following quick and dirty code. I'm sure you can improve to have it fit your needs.

@app.route('/')
def index():
    if request.authorization and request.authorization.username == 'user1' and request.authorization.password == 'pass1':
        return '<h1>You are logged in</h1> <a href="{{ url_for('index2') }}">Click me to go to index2</a>'

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

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

Upvotes: 0

Related Questions