0
0
Djangoframework~15 mins

ALLOWED_HOSTS configuration in Django - Mini Project: Build & Apply

Choose your learning style9 modes available
ALLOWED_HOSTS Configuration in Django
📖 Scenario: You are setting up a Django web application that will be accessed from specific domain names. To keep your app secure, you need to configure which hosts are allowed to serve your site.
🎯 Goal: Configure the ALLOWED_HOSTS setting in Django's settings.py file to specify the exact domain names your app will respond to.
📋 What You'll Learn
Create a list variable named ALLOWED_HOSTS in settings.py
Include the exact hostnames 'localhost' and 'example.com' in the list
Add a variable debug_mode set to False to simulate production environment
Use a conditional statement to include a warning comment if debug_mode is False and ALLOWED_HOSTS is empty
💡 Why This Matters
🌍 Real World
Web developers must configure ALLOWED_HOSTS in Django to prevent HTTP Host header attacks and ensure the app only responds to trusted domains.
💼 Career
Understanding ALLOWED_HOSTS is essential for Django developers to deploy secure and reliable web applications in production.
Progress0 / 4 steps
1
Create the ALLOWED_HOSTS list
In settings.py, create a list variable called ALLOWED_HOSTS with the exact values 'localhost' and 'example.com'.
Django
Need a hint?

Remember, ALLOWED_HOSTS is a list of strings.

2
Add a debug_mode variable
Below the ALLOWED_HOSTS list, create a variable called debug_mode and set it to False.
Django
Need a hint?

This variable simulates whether the app is in debug mode or production.

3
Check ALLOWED_HOSTS when debug_mode is false
Write an if statement that checks if debug_mode is False and ALLOWED_HOSTS is empty. Inside the if, write a comment saying # Warning: ALLOWED_HOSTS is empty in production.
Django
Need a hint?

Use and to combine conditions in the if statement.

4
Add localhost to ALLOWED_HOSTS if empty in production
Add an else block after the if statement. Inside the else, append 'localhost' to the ALLOWED_HOSTS list.
Django
Need a hint?

The append() method adds an item to the end of a list.