0Pricing
Django Academy · レッスン

DEBUG、SECRET_KEY、ALLOWED_HOSTS

危険な設定を正しく行います

「DEBUG、SECRET_KEY、ALLOWED_HOSTS」はCoddyKit上の無料Django Academyレッスンです。 これはレッスン1/4です。 下記で完全なレッスンを無料で読むことができます。その後、ブラウザ内の組み込みコードエディタと24時間対応のAIチューターでハンズオン演習できます。 これはDjango Academy学習パスの一部であり、ウェブとCoddyKitアプリ全体で進捗が同期されます。 Django Academyコースには全4レッスンが含まれています。

このレッスンの一部はまだ翻訳されておらず、英語で表示されています。

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

よくある質問

「DEBUG、SECRET_KEY、ALLOWED_HOSTS」レッスンは無料ですか?

はい。「DEBUG、SECRET_KEY、ALLOWED_HOSTS」の完全なテキストはこのウェブで無料で読めます。インタラクティブに演習し(組み込みコードエディタと24時間対応のAIチューター)、Django Academyコースの残りをアンロックするには、CoddyKit PROにアップグレードしてください。 Django Academyコースには全4レッスンが含まれています。

「DEBUG、SECRET_KEY、ALLOWED_HOSTS」で何を学びますか?

危険な設定を正しく行います ブラウザで直接実行するハンズオンコードでDjango Academyを演習し、24時間対応のAIチューターがレッスンを進める中での質問に答えます。

Django Academyを始めるのに経験は必要ですか?

事前経験は必要ありません。CoddyKitのDjango Academyは初級者から上級者向けに構成されているため、ここから始めるか最初から始めて、自分のペースで進むことができます。 これはレッスン1/4です。

「DEBUG、SECRET_KEY、ALLOWED_HOSTS」レッスンにはどのくらい時間がかかりますか?

ほとんどのCoddyKitレッスンは約5~10分かかります。各レッスンはコンパクトでインタラクティブなので、着実に進歩し、ウェブとアプリ全体で正確に前回の場所から再開できます。

このDjango Academyレッスンでコードを書いて実行できますか?

はい。すべてのDjango Academyレッスンに組み込みコードエディタが含まれているため、ブラウザでリアルコードを書いて実行し、即座のAIフィードバックを取得できます。ローカル設定は不要です。

このコースのすべてのレッスン

  1. DEBUG、SECRET_KEY、ALLOWED_HOSTS
  2. HTTPS、HSTS、Secure Cookies
  3. XSS、CSRF、SQL Injection対策
  4. Deployment Checklistの実行
← Django Academyに戻る