Django Academy · Leçon

DEBUG, SECRET_KEY et ALLOWED_HOSTS

Configurez correctement les paramètres dangereux.

Leçon 1 sur 413 étapes

DEBUG, SECRET_KEY et ALLOWED_HOSTS est une leçon Django Academy gratuite sur CoddyKit. Ceci est la leçon 1 sur 4. Tu peux lire la leçon complète ci-dessous gratuitement — puis la pratiquer en direct dans le navigateur avec un éditeur de code intégré et un tuteur IA 24/7. Elle fait partie du parcours d'apprentissage Django Academy, et ta progression se synchronise sur le web et l'application CoddyKit. Le cours Django Academy comprend 4 leçons au total.

Certaines parties de cette leçon n'ont pas encore été traduites et s'affichent en anglais.

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 = True

Turn 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 = False

Meet 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 --deploy

Quick 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. 🎉

Gratuit pour commencer

Apprends Python avec un tuteur IA — gratuit

Écris et exécute du vrai code dans ton navigateur, obtiens de l'aide instantanée d'un tuteur IA disponible 24h/24, et reprends là où tu t'es arrêté sur le web ou dans l'app.

Cours
30
Leçons
120

Questions Fréquemment Posées

La leçon « DEBUG, SECRET_KEY et ALLOWED_HOSTS » est-elle gratuite ?

Oui — le texte complet de « DEBUG, SECRET_KEY et ALLOWED_HOSTS » est gratuit à lire ici sur le web. Pour la pratiquer de manière interactive (un éditeur de code intégré et un tuteur IA 24/7) et déverrouiller le reste du cours Django Academy, passe à CoddyKit PRO. Le cours Django Academy comprend 4 leçons au total.

Qu'est-ce que j'apprendrai dans « DEBUG, SECRET_KEY et ALLOWED_HOSTS » ?

Configurez correctement les paramètres dangereux. Tu pratiques Django Academy avec du code pratique que tu exécutes directement dans le navigateur, et un tuteur IA 24/7 répond à tes questions au fur et à mesure que tu avances dans la leçon.

Dois-je avoir de l'expérience pour commencer Django Academy ?

Aucune expérience préalable n'est requise. Django Academy sur CoddyKit est structuré pour les débutants jusqu'aux apprenants avancés, donc tu peux commencer ici ou depuis le début et avancer à ton rythme. Ceci est la leçon 1 sur 4.

Combien de temps prend la leçon « DEBUG, SECRET_KEY et ALLOWED_HOSTS » ?

La plupart des leçons CoddyKit prennent environ 5–10 minutes. Chacune est courte et interactive, tu progresses régulièrement et tu repiques exactement où tu t'es arrêté sur le web et l'app.

Peux-tu écrire et exécuter du code dans cette leçon Django Academy ?

Oui. Chaque leçon Django Academy inclut un éditeur de code intégré, tu écris et exécutes du vrai code directement dans ton navigateur et tu reçois des retours IA instantanés — aucune configuration locale requise.

Toutes les leçons de ce cours

  1. DEBUG, SECRET_KEY et ALLOWED_HOSTS
  2. HTTPS, HSTS et cookies sécurisés
  3. Défenses contre XSS, CSRF et l’injection SQL
  4. Exécuter la liste de contrôle du déploiement
← Retour à Django Academy