Challenge - 5 Problems
Flask Redirect & Abort Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate2:00remaining
What happens when this Flask route is accessed?
Consider this Flask route:
What will the browser do when a user visits
from flask import Flask, redirect
app = Flask(__name__)
@app.route('/go')
def go():
return redirect('/target')What will the browser do when a user visits
/go?Flask
from flask import Flask, redirect app = Flask(__name__) @app.route('/go') def go(): return redirect('/target')
Attempts:
2 left
💡 Hint
Think about what the redirect function does in Flask.
✗ Incorrect
The redirect function sends a 302 HTTP response telling the browser to load the new URL '/target'. It does not require the '/target' route to be defined for the redirect to happen.
📝 Syntax
intermediate2:00remaining
Which option correctly aborts with a 404 error in Flask?
You want to stop processing and return a 404 Not Found error in a Flask route. Which code snippet does this correctly?
Flask
from flask import abort @app.route('/item/<int:id>') def item(id): if id != 1: # abort here return 'Item found'
Attempts:
2 left
💡 Hint
The abort function expects an integer status code.
✗ Incorrect
abort(404) correctly raises a 404 HTTP error. Passing a string or a different status code will either cause an error or return a different HTTP status.
❓ state_output
advanced2:00remaining
What is the HTTP status code returned by this route?
Analyze this Flask route:
What HTTP status code will the client receive when accessing '/check'?
from flask import Flask, abort, redirect
app = Flask(__name__)
@app.route('/check')
def check():
if False:
return redirect('/success')
else:
abort(403)What HTTP status code will the client receive when accessing '/check'?
Flask
from flask import Flask, abort, redirect app = Flask(__name__) @app.route('/check') def check(): if False: return redirect('/success') else: abort(403)
Attempts:
2 left
💡 Hint
Look at the condition and which branch runs.
✗ Incorrect
Since the condition is always False, the else branch runs, calling abort(403), which returns a 403 Forbidden HTTP status code.
🔧 Debug
advanced2:00remaining
Why does this Flask code raise an error?
Look at this Flask route:
What error will this code cause and why?
from flask import abort
@app.route('/data')
def data():
abort('404')What error will this code cause and why?
Flask
from flask import abort @app.route('/data') def data(): abort('404')
Attempts:
2 left
💡 Hint
Check the type of the argument passed to abort.
✗ Incorrect
abort expects an integer HTTP status code. Passing a string like '404' causes a TypeError.
🧠 Conceptual
expert2:00remaining
What is the difference between redirect and abort in Flask?
Choose the best explanation of how
redirect and abort functions behave in Flask.Attempts:
2 left
💡 Hint
Think about what happens to the browser in each case.
✗ Incorrect
redirect tells the browser to load a different URL (usually with 302 status). abort stops the request and returns an HTTP error code like 404 or 403.