2. Which of the following is the correct way to set ALLOWED_HOSTS to allow only example.com and localhost?
easy
A. ALLOWED_HOSTS = ['example.com', 'localhost']
B. ALLOWED_HOSTS = 'example.com, localhost'
C. ALLOWED_HOSTS = {example.com, localhost}
D. ALLOWED_HOSTS = (example.com, 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 A
Quick Check:
ALLOWED_HOSTS is a list = C [OK]
Hint: Use square brackets for ALLOWED_HOSTS list [OK]
Common Mistakes:
Using a string instead of a list
Using curly braces without quotes (syntax error)
Using parentheses without quotes (syntax error)
3. Given this settings.py snippet:
ALLOWED_HOSTS = ['myapp.com', 'localhost']
What happens if a request comes from evil.com?
medium
A. Django will raise a DisallowedHost error.
B. Django will serve the request normally.
C. Django will redirect to localhost.
D. Django will ignore the ALLOWED_HOSTS setting.
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 A
Hint: Requests from unknown hosts cause DisallowedHost error [OK]
Common Mistakes:
Assuming Django serves unknown hosts
Thinking Django redirects unknown hosts
Ignoring ALLOWED_HOSTS effect
4. Identify the error in this settings.py snippet:
ALLOWED_HOSTS = example.com, localhost
medium
A. ALLOWED_HOSTS should be a dictionary.
B. ALLOWED_HOSTS should be a string, not a list.
C. The list syntax is correct; no error.
D. Missing quotes around domain names.
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 D
Quick Check:
Domain names need quotes = A [OK]
Hint: Always quote domain names in ALLOWED_HOSTS list [OK]
Common Mistakes:
Forgetting quotes around domains
Using wrong data types like dict
Assuming ALLOWED_HOSTS can be a plain string
5. You want your Django app to accept requests from any domain during development but restrict to mydomain.com in production. Which ALLOWED_HOSTS setup is best?
hard
A. Set ALLOWED_HOSTS = ['localhost'] in production.
B. Set ALLOWED_HOSTS = [] in development and ['*'] in production.
C. Set ALLOWED_HOSTS = ['*'] in development and ['mydomain.com'] in production.
D. Set ALLOWED_HOSTS = ['mydomain.com'] always.
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 C
Quick Check:
Wildcard for dev, specific domain for prod = D [OK]
Hint: Use '*' for dev, specific domains for production [OK]
Common Mistakes:
Using empty list in development causes errors
Allowing all hosts in production is unsafe
Setting localhost only in production blocks real users
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.