0
0
Flaskframework~10 mins

Custom error pages (404, 500) 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 import the Flask class from the flask module.

Flask
from flask import [1]
app = Flask(__name__)
Drag options to blanks, or click blank then click option'
Arequest
Brender_template
CFlask
Dredirect
Attempts:
3 left
💡 Hint
Common Mistakes
Importing render_template instead of Flask
Using lowercase flask instead of Flask
2fill in blank
medium

Complete the code to define a custom 404 error handler using the decorator.

Flask
@app.errorhandler([1])
def not_found(error):
    return 'Page not found', 404
Drag options to blanks, or click blank then click option'
A404
B500
C403
D400
Attempts:
3 left
💡 Hint
Common Mistakes
Using 500 instead of 404 for not found errors
Using string '404' instead of integer 404
3fill in blank
hard

Fix the error in the code to return a custom 500 error page with a template.

Flask
@app.errorhandler(500)
def server_error(error):
    return [1]('500.html'), 500
Drag options to blanks, or click blank then click option'
Aredirect
Babort
Crequest
Drender_template
Attempts:
3 left
💡 Hint
Common Mistakes
Using redirect instead of render_template
Forgetting to import render_template
4fill in blank
hard

Fill both blanks to create a 404 error handler that returns a template and status code.

Flask
@app.errorhandler([1])
def page_not_found(error):
    return [2]('404.html'), 404
Drag options to blanks, or click blank then click option'
A404
Brender_template
Credirect
D500
Attempts:
3 left
💡 Hint
Common Mistakes
Mixing up error codes 404 and 500
Using redirect instead of render_template
5fill in blank
hard

Fill all three blanks to create a Flask app with custom 404 and 500 error handlers returning templates.

Flask
from flask import Flask, [1]

app = Flask(__name__)

@app.errorhandler(404)
def not_found(error):
    return [2]('404.html'), 404

@app.errorhandler(500)
def server_error(error):
    return [3]('500.html'), 500
Drag options to blanks, or click blank then click option'
Arender_template
Bredirect
Dabort
Attempts:
3 left
💡 Hint
Common Mistakes
Using redirect instead of render_template
Not importing render_template