What is the purpose of a custom 404 error page in a Flask app?
A custom 404 error page shows a friendly message when a user tries to visit a page that does not exist. It improves user experience by guiding them back to valid pages instead of showing a generic error.
Click to reveal answer
beginner
How do you define a custom error handler for a 500 Internal Server Error in Flask?
Use the @app.errorhandler(500) decorator on a function that returns a response or template. This function runs when a server error happens, letting you show a custom message or page.
Click to reveal answer
beginner
Which Flask function is used to render a custom error page template?
The render_template() function is used to load and show an HTML template for the error page, allowing you to design a nice layout and message.
Click to reveal answer
intermediate
Why is it important to return the correct HTTP status code with a custom error page?
Returning the correct status code (like 404 or 500) helps browsers and search engines understand the error type. It also helps tools and users know the page is an error, not a normal page.
Click to reveal answer
beginner
What happens if you don't define a custom error handler for 404 in Flask?
Flask shows a default plain text 404 error page. It works but looks basic and may confuse users. Custom pages improve clarity and keep your site style consistent.
Click to reveal answer
Which decorator is used to create a custom 404 error page in Flask?
A@app.errorhandler(404)
B@app.route('/404')
C@app.handle_error(404)
D@app.custom_error(404)
✗ Incorrect
The @app.errorhandler(404) decorator tells Flask to use the decorated function when a 404 error occurs.
What should a custom error handler function return?
AA rendered template and the error status code
BOnly a string message
CNothing, just print an error
DRedirect to homepage
✗ Incorrect
The function should return a rendered template (HTML) and the correct HTTP status code to properly show the error page.
What HTTP status code is used for Internal Server Error?
A404
B500
C403
D200
✗ Incorrect
500 is the standard HTTP status code for Internal Server Error.
If you want to show a custom page for any error, which decorator can you use?
A@app.route('/error')
B@app.errorhandler(404)
C@app.errorhandler(Exception)
D@app.catch_all()
✗ Incorrect
Using @app.errorhandler(Exception) catches all exceptions and can be used to show a generic error page.
Why should custom error pages be user-friendly?
ATo confuse users
BTo hide errors completely
CTo slow down the website
DTo help users understand what went wrong and guide them
✗ Incorrect
User-friendly error pages help users know what happened and how to continue, improving their experience.
Explain how to create a custom 404 error page in Flask and why it is useful.
Think about how Flask handles errors and how you can show a nicer page.
You got /4 concepts.
Describe the difference between default Flask error pages and custom error pages.
Consider what users see and how it affects their experience.
You got /4 concepts.
Practice
(1/5)
1. What is the main purpose of creating custom error pages like 404 or 500 in a Flask app?
easy
A. To show friendly messages instead of default error text
B. To speed up the server response time
C. To disable error logging
D. To automatically fix bugs in the code
Solution
Step 1: Understand default error behavior
By default, Flask shows plain error messages like '404 Not Found' which are not user-friendly.
Step 2: Purpose of custom error pages
Custom error pages replace these default messages with nicer, clearer pages that help users understand the problem.
Final Answer:
To show friendly messages instead of default error text -> Option A
Quick Check:
Custom error pages = Friendly messages [OK]
Hint: Custom error pages improve user experience with friendly messages [OK]
Common Mistakes:
Thinking custom pages fix server speed
Believing they disable error logs
Assuming they auto-correct bugs
2. Which of the following is the correct way to define a custom 404 error handler in Flask?
easy
A. @app.handle_error(404)
def not_found(error):
return 'Page not found', 404
B. @app.route('/404')
def not_found():
return 'Page not found', 404
C. @app.errorhandler(404)
def not_found(error):
return 'Page not found', 404
D. @app.error(404)
def not_found(error):
return 'Page not found', 404
Solution
Step 1: Identify correct decorator for error handling
Flask uses @app.errorhandler(error_code) to register error handlers.
Step 2: Check function signature and return
The handler function takes an error argument and returns a response with the error code.
Final Answer:
@app.errorhandler(404)\ndef not_found(error):\n return 'Page not found', 404 -> Option C
Quick Check:
@app.errorhandler(404) = Correct syntax [OK]
Hint: Use @app.errorhandler(code) to define error handlers [OK]
Common Mistakes:
Using @app.route instead of @app.errorhandler
Wrong decorator names like @app.handle_error
Missing error parameter in handler function
3. Given this Flask code snippet, what will be the output when a user visits a non-existing page?
A. The handler function must accept an error parameter
B. The decorator should be @app.handle_error(500)
C. The return statement must not include the status code
D. The function name must be 'handle_500_error'
Solution
Step 1: Check function signature for error handlers
Flask error handlers must accept one argument for the error object.
Step 2: Verify the given function signature
The function internal_error() has no parameters, which is incorrect for error handlers.
Final Answer:
The handler function must accept an error parameter -> Option A
Quick Check:
Error handler functions need error argument [OK]
Hint: Error handlers always take one error argument [OK]
Common Mistakes:
Omitting the error parameter in handler function
Using wrong decorator names
Thinking function name matters for error handlers
5. You want to create a custom 404 error page that renders an HTML template named '404.html' and returns status 404. Which Flask code snippet correctly does this?
hard
A. @app.route('/404')
def not_found():
return render_template('404.html'), 404
B. @app.errorhandler(404)
def not_found(error):
return render_template('404.html'), 404
C. @app.errorhandler(404)
def not_found():
return render_template('404.html')
D. @app.errorhandler(404)
def not_found(error):
return render_template('404.html', status=404)
Solution
Step 1: Use correct decorator and function signature
Use @app.errorhandler(404) and define function with error parameter.
Step 2: Return rendered template with status code
Return render_template('404.html') and the status code 404 as a tuple.
Final Answer:
@app.errorhandler(404)\ndef not_found(error):\n return render_template('404.html'), 404 -> Option B
Quick Check:
Custom 404 with template and status code = @app.errorhandler(404)
def not_found(error):
return render_template('404.html'), 404 [OK]
Hint: Return (render_template, status_code) tuple in error handler [OK]
Common Mistakes:
Using @app.route instead of @app.errorhandler
Missing error parameter in function
Not returning status code with template
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.