📖 Scenario: You are building a simple Django project. You want to learn how the DEBUG setting affects your project during development.
🎯 Goal: Create a Django settings snippet that sets up the DEBUG mode, configure allowed hosts, and understand how Django behaves differently when DEBUG is True or False.
📋 What You'll Learn
Create a variable called DEBUG and set it to True
Create a list called ALLOWED_HOSTS with one entry: 'localhost'
Add a conditional block that prints 'Debug mode is ON' if DEBUG is True
Add a conditional block that prints 'Debug mode is OFF' if DEBUG is False
💡 Why This Matters
🌍 Real World
Developers use the <code>DEBUG</code> setting to control whether Django shows detailed error messages and debugging information during development.
💼 Career
Understanding <code>DEBUG</code> mode is essential for Django developers to safely develop and deploy web applications.
Progress0 / 4 steps
1
Set DEBUG mode to true
Create a variable called DEBUG and set it to True in your Django settings.
Django
Hint
Use DEBUG = True to enable debug mode.
2
Configure ALLOWED_HOSTS
Create a list called ALLOWED_HOSTS with one string entry: 'localhost'.
Django
Hint
Set ALLOWED_HOSTS to a list with 'localhost' inside.
3
Add conditional message for DEBUG true
Add an if statement that checks if DEBUG is True and inside it write print('Debug mode is ON').
Django
Hint
Use if DEBUG: to check the debug mode and print the message inside.
4
Add conditional message for DEBUG false
Add an else block after the if DEBUG that prints 'Debug mode is OFF'.
Django
Hint
Use else: to handle the case when DEBUG is False.
Practice
(1/5)
1. What is the main purpose of setting DEBUG = True in a Django project?
easy
A. To enable database backups automatically
B. To improve website speed in production
C. To disable all logging
D. To show detailed error messages during development
Solution
Step 1: Understand the role of DEBUG in Django
DEBUG mode is designed to help developers by showing detailed error messages and debugging information.
Step 2: Identify the correct purpose
Setting DEBUG = True enables detailed error pages, which is useful only during development, not production.
Final Answer:
To show detailed error messages during development -> Option D
Quick Check:
DEBUG = True shows errors [OK]
Hint: DEBUG = True means show errors for fixing [OK]
Common Mistakes:
Thinking DEBUG improves production speed
Confusing DEBUG with logging settings
Assuming DEBUG enables backups
2. Which of the following is the correct way to disable debug mode in Django's settings.py?
easy
A. DEBUG = False
B. DEBUG = 'False'
C. debug = False
D. DEBUG = 0
Solution
Step 1: Check the correct syntax for boolean in Python
In Python, boolean values are capitalized: True or False without quotes.
Step 2: Identify the correct assignment in settings.py
DEBUG must be assigned the boolean False, not a string or lowercase variable.
Final Answer:
DEBUG = False -> Option A
Quick Check:
Boolean False without quotes disables debug [OK]
Hint: Use capitalized False without quotes to disable debug [OK]
Common Mistakes:
Using 'False' as a string instead of boolean
Using lowercase debug variable
Assigning numeric 0 instead of False
3. Given the following Django settings snippet:
DEBUG = True
ALLOWED_HOSTS = []
What will happen if you access the site from a browser?
medium
A. The site will redirect to a login page
B. The site will show a detailed error page if an error occurs
C. The site will run normally without errors
D. The site will block all hosts and show a 400 error
Solution
Step 1: Understand DEBUG and ALLOWED_HOSTS interaction
When DEBUG is True, Django ignores ALLOWED_HOSTS restrictions and shows detailed errors.
Step 2: Predict behavior on error
If an error occurs, Django will display a detailed debug page to help fix it.
Final Answer:
The site will show a detailed error page if an error occurs -> Option B