What if your website could automatically reject fake domains trying to pretend to be yours?
Why ALLOWED_HOSTS configuration in Django? - Purpose & Use Cases
Imagine you deploy your Django website and suddenly anyone can access it from any domain, including fake or harmful ones.
You try to manually check every incoming request's domain to block bad ones.
Manually checking domains is slow and easy to forget.
This can let attackers spoof your site or cause security problems.
It's hard to keep track of all allowed domains by hand.
Django's ALLOWED_HOSTS setting lets you list trusted domains.
The framework automatically blocks requests from unknown hosts.
This keeps your site safe without extra code.
if request.get_host() not in ['mydomain.com', 'www.mydomain.com']: return HttpResponseForbidden()
ALLOWED_HOSTS = ['mydomain.com', 'www.mydomain.com'] # Django blocks others automatically
You can safely deploy your Django app knowing only trusted domains can serve it.
A company launches their website and sets ALLOWED_HOSTS to their official domains to prevent phishing or misuse.
Manually checking hosts is error-prone and insecure.
ALLOWED_HOSTS centralizes trusted domains in one place.
Django automatically blocks requests from unknown hosts, improving security.
Practice
ALLOWED_HOSTS setting in a Django project?Solution
Step 1: Understand the role of ALLOWED_HOSTS
ALLOWED_HOSTS is a security setting that lists domains your Django app accepts requests from.Step 2: Compare with other settings
Database, static files, and middleware are configured elsewhere, not in ALLOWED_HOSTS.Final Answer:
It specifies which domain names the Django app can serve. -> Option BQuick Check:
ALLOWED_HOSTS controls allowed domains = A [OK]
- Confusing ALLOWED_HOSTS with database settings
- Thinking it controls static files
- Assuming it manages middleware
ALLOWED_HOSTS to allow only example.com and localhost?Solution
Step 1: Identify the correct data type for ALLOWED_HOSTS
ALLOWED_HOSTS must be a list of strings, so square brackets [] are correct.Step 2: Check each option's syntax
A uses curly braces with unquoted domains (syntax error); B is a string; D uses parentheses with unquoted domains (syntax error). Only C uses a list of quoted strings correctly. Django expects a list.Final Answer:
ALLOWED_HOSTS = ['example.com', 'localhost'] -> Option AQuick Check:
ALLOWED_HOSTS is a list = C [OK]
- Using a string instead of a list
- Using curly braces without quotes (syntax error)
- Using parentheses without quotes (syntax error)
settings.py snippet:ALLOWED_HOSTS = ['myapp.com', 'localhost']
What happens if a request comes from
evil.com?Solution
Step 1: Understand ALLOWED_HOSTS behavior
Requests from domains not in ALLOWED_HOSTS cause Django to raise a DisallowedHost exception.Step 2: Apply to the given domain
Since 'evil.com' is not in ['myapp.com', 'localhost'], Django blocks the request with an error.Final Answer:
Django will raise a DisallowedHost error. -> Option AQuick Check:
Unknown domain request = DisallowedHost error [OK]
- Assuming Django serves unknown hosts
- Thinking Django redirects unknown hosts
- Ignoring ALLOWED_HOSTS effect
settings.py snippet:ALLOWED_HOSTS = example.com, localhost
Solution
Step 1: Check syntax for ALLOWED_HOSTS
ALLOWED_HOSTS must be a list of strings, so domain names need quotes.Step 2: Identify the error in the snippet
example.com and localhost are unquoted, causing a syntax error or NameError.Final Answer:
Missing quotes around domain names. -> Option DQuick Check:
Domain names need quotes = A [OK]
- Forgetting quotes around domains
- Using wrong data types like dict
- Assuming ALLOWED_HOSTS can be a plain string
mydomain.com in production. Which ALLOWED_HOSTS setup is best?Solution
Step 1: Understand wildcard usage in development
Using ['*'] allows all hosts, useful for development to avoid host errors.Step 2: Restrict hosts in production
For security, production should specify exact domains like ['mydomain.com'] to block unwanted hosts.Final Answer:
Set ALLOWED_HOSTS = ['*'] in development and ['mydomain.com'] in production. -> Option CQuick Check:
Wildcard for dev, specific domain for prod = D [OK]
- Using empty list in development causes errors
- Allowing all hosts in production is unsafe
- Setting localhost only in production blocks real users
Ready to prove it?
Pass the test and this pattern moves unknown
