📖 Scenario: You are building a simple Flask web app. You want to see detailed error pages when something goes wrong during development. This helps you find and fix bugs faster.
🎯 Goal: Create a Flask app that shows debug mode error pages when an error happens. You will set up the app, enable debug mode, create a route that causes an error, and run the app so you can see the detailed error page.
📋 What You'll Learn
Create a Flask app instance named app
Enable debug mode by setting app.debug = True
Create a route /cause-error that raises a ZeroDivisionError
Run the app with app.run() so debug mode error pages appear
💡 Why This Matters
🌍 Real World
Developers use debug mode error pages to quickly find and fix bugs during app development.
💼 Career
Understanding debug mode is essential for backend developers working with Flask to build reliable web applications.
Progress0 / 4 steps
1
Create the Flask app instance
Import Flask from flask and create a Flask app instance called app.
Flask
Hint
Use Flask(__name__) to create the app instance.
2
Enable debug mode
Set the debug attribute of the app instance to True to enable debug mode.
Flask
Hint
Set app.debug = True to turn on debug mode.
3
Create a route that causes an error
Define a route /cause-error using @app.route. Inside the view function cause_error, raise a ZeroDivisionError by dividing 1 by 0.
Flask
Hint
Use @app.route('/cause-error') and define cause_error() that returns 1 / 0.
4
Run the Flask app
Add the code to run the Flask app by calling app.run() at the bottom of the file.
Flask
Hint
Call app.run() to start the Flask development server.
Practice
(1/5)
1. What does enabling debug mode in a Flask app do?
easy
A. Disables all error messages
B. Automatically fixes bugs in the code
C. Shows detailed error pages with debugging information
D. Prevents the app from restarting on code changes
Solution
Step 1: Understand debug mode purpose
Debug mode helps developers by showing detailed error pages when something goes wrong.
Step 2: Compare options with debug mode behavior
Only Shows detailed error pages with debugging information correctly describes that debug mode shows detailed error pages with debugging info.
Final Answer:
Shows detailed error pages with debugging information -> Option C
Quick Check:
Debug mode = detailed error pages [OK]
Hint: Debug mode shows detailed errors, not hides them [OK]
Common Mistakes:
Thinking debug mode hides errors
Believing debug mode fixes bugs automatically
Confusing debug mode with production mode
2. Which of the following is the correct way to enable debug mode in Flask?
easy
A. app.run(debug=False)
B. app.debug = True
C. app.debug = False
D. app.config['DEBUG'] = False
Solution
Step 1: Recall how to enable debug mode
Debug mode can be enabled by setting app.debug = True or app.config['DEBUG'] = True.
Step 2: Identify the correct option
Only app.debug = True sets debug mode to True correctly.
Final Answer:
app.debug = True -> Option B
Quick Check:
Enable debug = app.debug = True [OK]
Hint: Set app.debug = True to enable debug mode [OK]
Common Mistakes:
Setting debug to False instead of True
Confusing app.run debug argument with app.debug property
Using incorrect config keys
3. Given this Flask code snippet, what will you see if an error occurs?
from flask import Flask
app = Flask(__name__)
app.debug = True
@app.route('/')
def home():
return 1 / 0 # This will cause an error
app.run()
medium
A. A detailed error page showing the division by zero traceback
B. A blank page with no error message
C. A generic 500 Internal Server Error page without details
D. The app will crash and stop running
Solution
Step 1: Analyze debug mode effect on errors
With app.debug = True, Flask shows detailed error pages including tracebacks.
Step 2: Understand the error in the code
The route causes a division by zero error, triggering the debug error page.
Final Answer:
A detailed error page showing the division by zero traceback -> Option A
Quick Check:
Debug mode shows detailed error page on exceptions [OK]
Hint: Debug True shows detailed error pages on exceptions [OK]
Common Mistakes:
Expecting a generic error page in debug mode
Thinking the app crashes completely
Assuming no error message is shown
4. What is wrong with this Flask app code if you want to see debug error pages?