Custom error pages help users understand when something goes wrong on your website. They make your site look friendly and professional instead of showing confusing default messages.
Custom error pages (404, 500) in Flask
from flask import Flask, render_template app = Flask(__name__) @app.errorhandler(404) def page_not_found(e): return render_template('404.html'), 404 @app.errorhandler(500) def internal_error(e): return render_template('500.html'), 500
Use @app.errorhandler(code) to define a function for each error code.
Return the error page template and the error code as a tuple.
from flask import Flask, render_template app = Flask(__name__) @app.errorhandler(404) def not_found(e): return render_template('404.html'), 404
from flask import Flask, render_template app = Flask(__name__) @app.errorhandler(500) def server_error(e): return render_template('500.html'), 500
This Flask app shows how to create custom pages for 404 and 500 errors. Visiting a missing page shows the 404 message. Visiting /cause500 triggers a server error to show the 500 message.
from flask import Flask, render_template_string, abort app = Flask(__name__) # Custom 404 error page @app.errorhandler(404) def page_not_found(e): return render_template_string('<h1>404 Not Found</h1><p>Sorry, the page you requested does not exist.</p>'), 404 # Custom 500 error page @app.errorhandler(500) def internal_error(e): return render_template_string('<h1>500 Internal Server Error</h1><p>Oops! Something went wrong on our side.</p>'), 500 @app.route('/') def home(): return '<h1>Welcome to the Home Page</h1>' @app.route('/cause500') def cause_error(): # This will trigger a 500 error abort(500) if __name__ == '__main__': app.run(debug=True)
Use render_template with HTML files for real projects instead of render_template_string.
Set debug=False in production to avoid showing default error pages.
Custom error pages improve user experience and help keep your site professional.
Custom error pages replace default error messages with friendly pages.
Use @app.errorhandler to define handlers for specific error codes.
Return the error page content and the error code together.
Practice
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 AQuick Check:
Custom error pages = Friendly messages [OK]
- Thinking custom pages fix server speed
- Believing they disable error logs
- Assuming they auto-correct bugs
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 CQuick Check:
@app.errorhandler(404) = Correct syntax [OK]
- Using @app.route instead of @app.errorhandler
- Wrong decorator names like @app.handle_error
- Missing error parameter in handler function
@app.errorhandler(404)
def page_not_found(e):
return 'Oops! Page missing.
', 404
@app.route('/')
def home():
return 'Welcome Home'Solution
Step 1: Understand route and error handler
The '/' route returns 'Welcome Home'. The 404 error handler returns a custom HTML message with status 404.Step 2: Accessing a non-existing page triggers 404
Visiting a page not defined triggers the 404 handler, so the custom message is returned with status 404.Final Answer:
The browser shows '<h1>Oops! Page missing.</h1>' with 404 status -> Option DQuick Check:
404 error triggers custom handler = The browser shows '<h1>Oops! Page missing.</h1>' with 404 status [OK]
- Thinking '/' route handles all pages
- Expecting default Flask 404 page instead of custom
- Assuming server crashes on 404
@app.errorhandler(500)
def internal_error():
return 'Server error occurred', 500Solution
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 AQuick Check:
Error handler functions need error argument [OK]
- Omitting the error parameter in handler function
- Using wrong decorator names
- Thinking function name matters for error handlers
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 BQuick Check:
Custom 404 with template and status code = @app.errorhandler(404) def not_found(error): return render_template('404.html'), 404 [OK]
- Using @app.route instead of @app.errorhandler
- Missing error parameter in function
- Not returning status code with template
Ready to prove it?
Pass the test and this pattern moves unknown
