Given this Flask app with a custom 404 error handler, what will the user see if they visit /unknown?
from flask import Flask, render_template_string app = Flask(__name__) @app.errorhandler(404) def page_not_found(e): return render_template_string('<h1>Page Not Found</h1><p>Sorry, no page here.</p>'), 404 @app.route('/') def home(): return 'Welcome Home!' if __name__ == '__main__': app.run()
Think about what Flask does when a route is not found and a custom 404 handler is defined.
When a user visits a route that does not exist, Flask triggers the 404 error handler. Since a custom handler is defined, Flask returns the custom HTML with status 404.
Which option correctly fixes the syntax error in this Flask 500 error handler?
from flask import Flask, render_template_string app = Flask(__name__) @app.errorhandler(500) def internal_error(e): return render_template_string('<h1>Server Error</h1><p>Something went wrong.</p>') 500 if __name__ == '__main__': app.run()
Remember how to return a tuple with response and status code in Flask.
Flask expects a tuple (response, status_code). The status code must be separated by a comma, not just placed after the return value.
Consider this Flask app snippet. What HTTP status code will the client receive when a 404 error occurs?
from flask import Flask, render_template_string app = Flask(__name__) @app.errorhandler(404) def not_found(e): return render_template_string('<h1>Oops!</h1>'), 200 if __name__ == '__main__': app.run()
Check the status code explicitly returned by the handler.
The handler returns a 200 status code explicitly, so the client receives 200 OK even though the route was not found.
Review this Flask app code. Why does the custom 404 page never show?
from flask import Flask app = Flask(__name__) @app.route('/hello') def hello(): return 'Hello!' @app.errorhandler(404) def page_not_found(): return '<h1>Not Found</h1>', 404 if __name__ == '__main__': app.run()
Check the function signature of error handlers in Flask.
Flask error handlers must accept the error as a parameter. Missing it causes the handler not to be called properly.
Choose the correct statement about how Flask handles custom error pages for 404 and 500 errors.
Think about what Flask expects from error handlers to replace default pages.
To override default error pages, Flask custom handlers must return a response and the matching HTTP status code. Otherwise, default pages show.