DEBUG, SECRET_KEY und ALLOWED_HOSTS
Gefährliche Einstellungen korrekt konfigurieren
DEBUG, SECRET_KEY und ALLOWED_HOSTS ist eine kostenlose Django Academy-Lektion auf CoddyKit. Dies ist Lektion 1 von 4. Du kannst die komplette Lektion unten kostenlos lesen – dann übst du sie direkt im Browser mit einem integrierten Code-Editor und einem KI-Tutor rund um die Uhr. Sie ist Teil des Django Academy-Lernpfads, und dein Fortschritt wird über Web und CoddyKit-App synchronisiert. Der Django Academy-Kurs umfasst insgesamt 4 Lektionen.
Teile dieser Lektion wurden noch nicht übersetzt und werden auf Englisch angezeigt.
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. 🎉
Häufig gestellte Fragen
Ist die Lektion „DEBUG, SECRET_KEY und ALLOWED_HOSTS“ kostenlos?
Ja — der vollständige Text von „DEBUG, SECRET_KEY und ALLOWED_HOSTS“ ist hier im Web kostenlos zu lesen. Um sie interaktiv zu üben (integrierter Code-Editor und 24/7 KI-Tutor) und den Rest des Django Academy-Kurses freizuschalten, upgrade auf CoddyKit PRO. Der Django Academy-Kurs umfasst insgesamt 4 Lektionen.
Was lerne ich in „DEBUG, SECRET_KEY und ALLOWED_HOSTS“?
Gefährliche Einstellungen korrekt konfigurieren Du übst Django Academy mit praktischem Code, den du direkt im Browser ausführst, und ein 24/7 KI-Tutor beantwortet deine Fragen während du die Lektion bearbeitest.
Brauche ich Erfahrung, um Django Academy zu starten?
Keine Vorkenntnisse erforderlich. Django Academy auf CoddyKit ist für Anfänger bis fortgeschrittene Lernende strukturiert, sodass du hier starten oder von Anfang an beginnen und in deinem eigenen Tempo voranschreiten kannst. Dies ist Lektion 1 von 4.
Wie lange dauert die Lektion „DEBUG, SECRET_KEY und ALLOWED_HOSTS“?
Die meisten CoddyKit-Lektionen dauern etwa 5–10 Minuten. Jede ist kompakt und interaktiv, sodass du stetig Fortschritte machst und genau dort weitermachst, wo du aufgehört hast – im Web und in der App.
Kann ich in dieser Django Academy-Lektion Code schreiben und ausführen?
Ja. Jede Django Academy-Lektion enthält einen integrierten Code-Editor, sodass du echten Code direkt in deinem Browser schreibst und ausführst und sofort KI-Feedback erhältst — ohne lokale Einrichtung erforderlich.
Alle Lektionen in diesem Kurs
- DEBUG, SECRET_KEY und ALLOWED_HOSTS
- HTTPS, HSTS und sichere Cookies
- Schutz vor XSS, CSRF und SQL-Injection
- Die Deployment-Checkliste durchführen