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)
We can use multiple decorators by stacking them.
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.
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.
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.
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)
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