Bird
Raised Fist0
Djangoframework~10 mins

SECRET_KEY and security settings in Django - Interactive Code Practice

Choose your learning style10 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to set a secret key in Django settings.

Django
SECRET_KEY = '[1]'
Drag options to blanks, or click blank then click option'
AINSTALLED_APPS
BDEBUG = True
CALLOWED_HOSTS = []
Dyour-secret-key-here
Attempts:
3 left
💡 Hint
Common Mistakes
Using settings like DEBUG or ALLOWED_HOSTS instead of a secret key string.
Leaving SECRET_KEY empty or as a placeholder.
2fill in blank
medium

Complete the code to disable debug mode in production.

Django
DEBUG = [1]
Drag options to blanks, or click blank then click option'
AFalse
B0
CTrue
DNone
Attempts:
3 left
💡 Hint
Common Mistakes
Setting DEBUG to True in production.
Using string 'False' instead of boolean False.
3fill in blank
hard

Fix the error in the allowed hosts setting to allow only example.com.

Django
ALLOWED_HOSTS = [[1]]
Drag options to blanks, or click blank then click option'
A'localhost'
B'example.com'
C'*'
Dexample.com
Attempts:
3 left
💡 Hint
Common Mistakes
Using unquoted domain names causing syntax errors.
Using '*' which allows all hosts (not secure).
4fill in blank
hard

Fill both blanks to set secure cookie settings in Django.

Django
SESSION_COOKIE_SECURE = [1]
CSRF_COOKIE_SECURE = [2]
Drag options to blanks, or click blank then click option'
ATrue
BFalse
CNone
D'True'
Attempts:
3 left
💡 Hint
Common Mistakes
Using string 'True' instead of boolean True.
Setting these to False in production.
5fill in blank
hard

Fill all three blanks to configure Django security settings for HTTPS and headers.

Django
SECURE_SSL_REDIRECT = [1]
SECURE_HSTS_SECONDS = [2]
SECURE_CONTENT_TYPE_NOSNIFF = [3]
Drag options to blanks, or click blank then click option'
ATrue
BFalse
C31536000
D0
Attempts:
3 left
💡 Hint
Common Mistakes
Setting SECURE_SSL_REDIRECT to False in production.
Using 0 for SECURE_HSTS_SECONDS disables HSTS.
Setting SECURE_CONTENT_TYPE_NOSNIFF to False.

Practice

(1/5)
1. What is the main purpose of the SECRET_KEY in a Django project?
easy
A. To define the database connection string
B. To secure sessions and cryptographic signing
C. To list allowed hosts for the app
D. To enable debug mode during development

Solution

  1. Step 1: Understand the role of SECRET_KEY

    The SECRET_KEY is used by Django to provide cryptographic signing, which secures sessions and other data.
  2. Step 2: Differentiate from other settings

    Database connection strings, allowed hosts, and debug mode serve different purposes unrelated to cryptographic security.
  3. Final Answer:

    To secure sessions and cryptographic signing -> Option B
  4. Quick Check:

    SECRET_KEY secures sessions = B [OK]
Hint: SECRET_KEY protects data and sessions, not database or hosts [OK]
Common Mistakes:
  • Confusing SECRET_KEY with database settings
  • Thinking SECRET_KEY controls allowed hosts
  • Assuming SECRET_KEY enables debug mode
2. Which of the following is the correct way to set a SECRET_KEY in Django's settings.py?
easy
A. SECRET_KEY = 'a-very-long-random-string'
B. SECRET_KEY = None
C. SECRET_KEY = True
D. SECRET_KEY = 1234567890

Solution

  1. Step 1: Check the type of SECRET_KEY

    The SECRET_KEY must be a long, random string enclosed in quotes (a string).
  2. Step 2: Evaluate each option

    SECRET_KEY = 1234567890 is a number, not a string. SECRET_KEY = True is a boolean. SECRET_KEY = None is None. Only SECRET_KEY = 'a-very-long-random-string' is a proper string.
  3. Final Answer:

    SECRET_KEY = 'a-very-long-random-string' -> Option A
  4. Quick Check:

    SECRET_KEY must be a string = D [OK]
Hint: SECRET_KEY must be a string, not number or boolean [OK]
Common Mistakes:
  • Setting SECRET_KEY as a number without quotes
  • Using boolean or null instead of a string
  • Using a short or guessable string
3. Given this settings.py snippet:
DEBUG = False
ALLOWED_HOSTS = ['example.com']
SECRET_KEY = 'mysecretkey'

What happens if a request comes from malicious.com?
medium
A. Django will crash with a server error
B. Django will accept the request and serve the page
C. Django will reject the request with a 400 Bad Request error
D. Django will redirect to example.com automatically

Solution

  1. Step 1: Understand ALLOWED_HOSTS role

    When DEBUG is false, Django only accepts requests from hosts listed in ALLOWED_HOSTS.
  2. Step 2: Check the request host

    The request is from malicious.com, which is not in ALLOWED_HOSTS, so Django rejects it with a 400 error.
  3. Final Answer:

    Django will reject the request with a 400 Bad Request error -> Option C
  4. Quick Check:

    Request host not allowed = 400 error = C [OK]
Hint: Requests from hosts not in ALLOWED_HOSTS get 400 error [OK]
Common Mistakes:
  • Thinking Django accepts all hosts when DEBUG is false
  • Assuming Django crashes on disallowed hosts
  • Believing Django redirects disallowed hosts
4. You set DEBUG = false but forgot to add your domain to ALLOWED_HOSTS. What error will you most likely see when accessing your site?
medium
A. No error, site loads normally
B. TemplateDoesNotExist error
C. Database connection error
D. DisallowedHost error

Solution

  1. Step 1: Understand ALLOWED_HOSTS enforcement

    When DEBUG is false, Django checks if the request host is in ALLOWED_HOSTS. If not, it raises a DisallowedHost error.
  2. Step 2: Identify the error from missing domain

    Since the domain is missing from ALLOWED_HOSTS, Django raises DisallowedHost.
  3. Final Answer:

    DisallowedHost error -> Option D
  4. Quick Check:

    Missing domain in ALLOWED_HOSTS = DisallowedHost error = A [OK]
Hint: Missing domain in ALLOWED_HOSTS causes DisallowedHost error [OK]
Common Mistakes:
  • Confusing DisallowedHost with template or database errors
  • Assuming site loads without ALLOWED_HOSTS set
  • Thinking DEBUG controls DisallowedHost error
5. You want to improve your Django app security. Which combination of settings is best practice for production?
hard
A. DEBUG = False, SECRET_KEY = 'a-long-random-string', ALLOWED_HOSTS = ['yourdomain.com']
B. DEBUG = True, SECRET_KEY = 'shortkey', ALLOWED_HOSTS = ['*']
C. DEBUG = False, SECRET_KEY = '', ALLOWED_HOSTS = []
D. DEBUG = True, SECRET_KEY = 'a-long-random-string', ALLOWED_HOSTS = []

Solution

  1. Step 1: Understand production security needs

    In production, DEBUG must be False to avoid exposing sensitive info. SECRET_KEY must be long and random. ALLOWED_HOSTS should list your domain to restrict access.
  2. Step 2: Evaluate each option

    DEBUG = True, SECRET_KEY = 'shortkey', ALLOWED_HOSTS = ['*'] has DEBUG = True and weak SECRET_KEY, unsafe. DEBUG = False, SECRET_KEY = '', ALLOWED_HOSTS = [] has empty SECRET_KEY and ALLOWED_HOSTS, insecure. DEBUG = True, SECRET_KEY = 'a-long-random-string', ALLOWED_HOSTS = [] has DEBUG = True and empty ALLOWED_HOSTS, unsafe. Only DEBUG = False, SECRET_KEY = 'a-long-random-string', ALLOWED_HOSTS = ['yourdomain.com'] meets all best practices.
  3. Final Answer:

    DEBUG = False, SECRET_KEY = 'a-long-random-string', ALLOWED_HOSTS = ['yourdomain.com'] -> Option A
  4. Quick Check:

    Production needs DEBUG false, strong SECRET_KEY, correct ALLOWED_HOSTS = A [OK]
Hint: Production: DEBUG false, strong SECRET_KEY, restrict ALLOWED_HOSTS [OK]
Common Mistakes:
  • Leaving DEBUG true in production
  • Using short or empty SECRET_KEY
  • Allowing all hosts with ALLOWED_HOSTS = ['*']
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard