Performance: SECRET_KEY and security settings
This affects the security and integrity of your Django application, indirectly impacting user trust and safe interaction speed.
import os SECRET_KEY = os.environ.get('DJANGO_SECRET_KEY') DEBUG = False ALLOWED_HOSTS = ['yourdomain.com']
SECRET_KEY = 'hardcoded-in-source-code' DEBUG = True ALLOWED_HOSTS = []
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Hardcoded SECRET_KEY with DEBUG=True | 0 | 0 | 0 | [X] Bad |
| SECRET_KEY from environment with DEBUG=False | 0 | 0 | 0 | [OK] Good |
SECRET_KEY in a Django project?SECRET_KEY is used by Django to provide cryptographic signing, which secures sessions and other data.SECRET_KEY in Django's settings.py?SECRET_KEY must be a long, random string enclosed in quotes (a string).settings.py snippet:DEBUG = False ALLOWED_HOSTS = ['example.com'] SECRET_KEY = 'mysecretkey'
malicious.com?DEBUG is false, Django only accepts requests from hosts listed in ALLOWED_HOSTS.malicious.com, which is not in ALLOWED_HOSTS, so Django rejects it with a 400 error.DEBUG = false but forgot to add your domain to ALLOWED_HOSTS. What error will you most likely see when accessing your site?DEBUG is false, Django checks if the request host is in ALLOWED_HOSTS. If not, it raises a DisallowedHost error.ALLOWED_HOSTS, Django raises DisallowedHost.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.Pass the test and this pattern moves unknown