0
0
Flaskframework~10 mins

Abort for intentional errors in Flask - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to abort with a 404 error in Flask.

Flask
from flask import Flask, [1]
app = Flask(__name__)

@app.route('/item/<int:id>')
def get_item(id):
    if id != 1:
        [1](404)
    return 'Item found'
Drag options to blanks, or click blank then click option'
Aabort
Brender_template
Credirect
Durl_for
Attempts:
3 left
💡 Hint
Common Mistakes
Using redirect instead of abort
Forgetting to import abort
2fill in blank
medium

Complete the code to abort with a 403 Forbidden error when access is denied.

Flask
from flask import Flask, abort
app = Flask(__name__)

@app.route('/admin')
def admin_panel():
    user_role = 'guest'
    if user_role != 'admin':
        [1](403)
    return 'Welcome admin!'
Drag options to blanks, or click blank then click option'
Aredirect
Babort
Cflash
Drender_template
Attempts:
3 left
💡 Hint
Common Mistakes
Using redirect instead of abort
Using flash which only shows messages
3fill in blank
hard

Fix the error in the code to abort with a 400 Bad Request error correctly.

Flask
from flask import Flask, abort
app = Flask(__name__)

@app.route('/submit')
def submit():
    data = None
    if data is None:
        [1](400)
    return 'Data received'
Drag options to blanks, or click blank then click option'
Aabort(404)
Babort()
Cabort
Dabort(400)
Attempts:
3 left
💡 Hint
Common Mistakes
Putting full call in blank causing syntax error
Using wrong status code
4fill in blank
hard

Fill both blanks to abort with a 500 Internal Server Error and import the correct function.

Flask
from flask import Flask, [1]
app = Flask(__name__)

@app.route('/error')
def error():
    [2](500)
    return 'This will not run'
Drag options to blanks, or click blank then click option'
Aabort
Bredirect
Drender_template
Attempts:
3 left
💡 Hint
Common Mistakes
Importing wrong function
Calling wrong function name
5fill in blank
hard

Fill all three blanks to abort with a 401 Unauthorized error, import the function, and use it correctly.

Flask
from flask import Flask, [1]
app = Flask(__name__)

@app.route('/login')
def login():
    authorized = False
    if not authorized:
        [2]([3])
    return 'Welcome!'
Drag options to blanks, or click blank then click option'
Aabort
C401
Dredirect
Attempts:
3 left
💡 Hint
Common Mistakes
Using redirect instead of abort
Using wrong status code