DEBUG, SECRET_KEY e ALLOWED_HOSTS
Configure corretamente as definições perigosas
DEBUG, SECRET_KEY e ALLOWED_HOSTS é uma aula grátis de Django Academy no CoddyKit. Esta é a aula 1 de 4. Você pode ler a aula completa abaixo gratuitamente — depois pratica ao vivo no navegador com um editor de código integrado e um tutor de IA 24/7. Faz parte do caminho de aprendizado de Django Academy, e seu progresso é sincronizado entre a web e o app CoddyKit. O curso de Django Academy inclui 4 aulas no total.
Partes desta aula ainda não foram traduzidas e aparecem em 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. 🎉
Perguntas Frequentes
A aula “DEBUG, SECRET_KEY e ALLOWED_HOSTS” é grátis?
Sim — o texto completo de “DEBUG, SECRET_KEY e ALLOWED_HOSTS” é grátis para ler aqui na web. Para praticá-la interativamente (um editor de código integrado e um tutor de IA 24/7) e desbloquear o restante do curso de Django Academy, atualize para CoddyKit PRO. O curso de Django Academy inclui 4 aulas no total.
O que vou aprender em “DEBUG, SECRET_KEY e ALLOWED_HOSTS”?
Configure corretamente as definições perigosas Você pratica Django Academy com código prático que executa diretamente no navegador, e um tutor de IA 24/7 responde suas dúvidas enquanto trabalha na aula.
Preciso ter experiência prévia para começar Django Academy?
Nenhuma experiência prévia é necessária. Django Academy no CoddyKit é estruturado para alunos iniciantes até avançados, então você pode começar aqui ou desde o início e aprender no seu ritmo. Esta é a aula 1 de 4.
Quanto tempo leva a aula “DEBUG, SECRET_KEY e ALLOWED_HOSTS”?
A maioria das aulas CoddyKit leva cerca de 5–10 minutos. Cada uma é compacta e interativa, então você faz progresso constante e retoma exatamente de onde parou entre web e app.
Posso escrever e executar código nesta aula de Django Academy?
Sim. Cada aula de Django Academy inclui um editor de código integrado, então você escreve e executa código real direto no navegador e recebe feedback de IA instantaneamente — nenhuma configuração local necessária.
Todas as aulas deste curso
- DEBUG, SECRET_KEY e ALLOWED_HOSTS
- HTTPS, HSTS e cookies seguros
- Defesas contra XSS, CSRF e injeção de SQL
- Executando a lista de verificação de implantação