DEBUG, SECRET_KEY y ALLOWED_HOSTS
Configure correctamente los ajustes peligrosos
DEBUG, SECRET_KEY y ALLOWED_HOSTS es una lección gratuita de Django Academy en CoddyKit. Esta es la lección 1 de 4. Puedes leer la lección completa abajo gratuitamente — luego la practicas en el navegador con un editor de código integrado y un tutor de IA 24/7. Forma parte de la ruta de aprendizaje de Django Academy, y tu progreso se sincroniza en la web y la app de CoddyKit. El curso de Django Academy incluye 4 lecciones en total.
Partes de esta lección aún no han sido traducidas y se muestran en inglés.
Three Dangerous Settings
Three settings in settings.py can sink a production site if you get them wrong. Let us tame DEBUG, SECRET_KEY, and ALLOWED_HOSTS together. 🔒
What DEBUG Does
When DEBUG is True, Django shows detailed error pages with your code, settings, and traceback. That is gold in dev but a gift to attackers in production.
DEBUG = TrueTurn DEBUG Off in Production
In production, set DEBUG to False so users see a generic 500 page instead of your internals. Never ship a live site with DEBUG on.
DEBUG = FalseMeet SECRET_KEY
The SECRET_KEY is the seed Django uses to sign sessions, CSRF tokens, and password resets. Leak it and an attacker can forge any of them.
SECRET_KEY = "django-insecure-..."Keep SECRET_KEY Secret
Never commit your real SECRET_KEY to git. Load it from an environment variable so the value lives outside your code.
import os
SECRET_KEY = os.environ["DJANGO_SECRET_KEY"]Generate a Strong Key
Need a fresh SECRET_KEY? Django ships a helper that returns a long, random, hard-to-guess string for you to store securely.
from django.core.management.utils import get_random_secret_key
get_random_secret_key()Why ALLOWED_HOSTS Exists
ALLOWED_HOSTS lists the domains your site is allowed to serve. It blocks HTTP Host header attacks that try to trick your app with a fake hostname.
Set Your Real Domains
Fill ALLOWED_HOSTS with the exact domains your site answers to. With DEBUG off, any request to an unlisted host gets a 400 error.
ALLOWED_HOSTS = ["example.com", "www.example.com"]Avoid the Wildcard Trap
Setting ALLOWED_HOSTS to the wildcard accepts any host header and defeats the protection. Use it only for quick local tests, never in production.
ALLOWED_HOSTS = ["*"]Drive It All From Env
The clean pattern is one source of truth: read DEBUG, SECRET_KEY, and hosts from environment variables so the same code runs safely everywhere.
DEBUG = os.environ.get("DEBUG", "0") == "1"Let Django Warn You
Django can audit these settings for you. The check --deploy command flags an unsafe DEBUG, weak key, or open hosts before you ship.
python manage.py check --deployQuick Check
Time to test your instinct about production settings.
Recap: The Safe Trio
You locked down the basics: DEBUG off, a secret key kept out of git, and ALLOWED_HOSTS scoped to your domains. Run check --deploy and you are off to a safe start. 🎉
Preguntas frecuentes
¿La lección «DEBUG, SECRET_KEY y ALLOWED_HOSTS» es gratis?
Sí — el texto completo de «DEBUG, SECRET_KEY y ALLOWED_HOSTS» es gratis para leer aquí en la web. Para practicarla de forma interactiva (editor de código integrado y tutor de IA 24/7) y desbloquear el resto del curso de Django Academy, actualiza a CoddyKit PRO. El curso de Django Academy incluye 4 lecciones en total.
¿Qué aprenderé en «DEBUG, SECRET_KEY y ALLOWED_HOSTS»?
Configure correctamente los ajustes peligrosos Practicas Django Academy con código real que ejecutas directamente en el navegador, y un tutor de IA 24/7 responde tus preguntas mientras trabajas en la lección.
¿Necesito experiencia previa para empezar Django Academy?
No se requiere experiencia previa. Django Academy en CoddyKit está estructurado para principiantes hasta estudiantes avanzados, así que puedes empezar aquí o desde el inicio y avanzar a tu ritmo. Esta es la lección 1 de 4.
¿Cuánto tiempo toma la lección «DEBUG, SECRET_KEY y ALLOWED_HOSTS»?
La mayoría de las lecciones de CoddyKit toman alrededor de 5–10 minutos. Cada una es compacta e interactiva, así que avanzas constantemente y retomas exactamente por donde dejaste en la web y la app.
¿Puedo escribir y ejecutar código en esta lección de Django Academy?
Sí. Cada lección de Django Academy incluye un editor de código integrado, así que escribes y ejecutas código real directamente en tu navegador y obtienes retroalimentación instantánea de IA — sin configuración local necesaria.
Todas las lecciones de este curso
- DEBUG, SECRET_KEY y ALLOWED_HOSTS
- HTTPS, HSTS y cookies seguras
- Defensas contra XSS, CSRF e inyección SQL
- Ejecución de la lista de comprobación de despliegue