0
0
Flaskframework~20 mins

Redirect and abort functions in Flask - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Flask Redirect & Abort Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What happens when this Flask route is accessed?
Consider this Flask route:
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')
AThe browser will show a 404 error because '/target' route is not defined.
BThe browser will be redirected to the URL '/target' with a 302 status code.
CThe server will return a JSON response with the URL '/target'.
DThe browser will stay on '/go' and display a blank page.
Attempts:
2 left
💡 Hint
Think about what the redirect function does in Flask.
📝 Syntax
intermediate
2: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'
Aabort(404)
Babort(404, 'Not Found')
Cabort('404')
Dabort(400)
Attempts:
2 left
💡 Hint
The abort function expects an integer status code.
state_output
advanced
2:00remaining
What is the HTTP status code returned by this route?
Analyze this Flask route:
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)
A404 Not Found
B302 Found
C403 Forbidden
D200 OK
Attempts:
2 left
💡 Hint
Look at the condition and which branch runs.
🔧 Debug
advanced
2:00remaining
Why does this Flask code raise an error?
Look at this Flask route:
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')
ANo error; it returns a 404 error correctly.
BSyntaxError due to missing parentheses in abort call.
CRuntimeError because abort cannot be called inside a route.
DTypeError because abort expects an integer status code, not a string.
Attempts:
2 left
💡 Hint
Check the type of the argument passed to abort.
🧠 Conceptual
expert
2:00remaining
What is the difference between redirect and abort in Flask?
Choose the best explanation of how redirect and abort functions behave in Flask.
Aredirect sends a response telling the browser to visit a new URL; abort stops processing and returns an HTTP error code.
Bredirect stops the server and returns an error; abort sends the browser to a new URL.
CBoth redirect and abort send the browser to a new URL but with different status codes.
Dredirect and abort are interchangeable and both return HTTP 404 errors.
Attempts:
2 left
💡 Hint
Think about what happens to the browser in each case.