0Pricing
Django Academy · درس

DEBUG وSECRET_KEY وALLOWED_HOSTS

تهيئة الإعدادات الحساسة بطريقة صحيحة

DEBUG وSECRET_KEY وALLOWED_HOSTS درس مجاني في Django Academy على CoddyKit. هذا هو الدرس 1 من أصل 4. يمكنك قراءة الدرس كاملاً أدناه مجاناً — ثم تمرن عليه مباشرة في المتصفح باستخدام محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7. هذا الدرس جزء من مسار التعلم في 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/7) وفتح باقي دورة Django Academy، انتقل إلى CoddyKit PRO. تتضمن دورة Django Academy 4 دروس في المجموع.

ماذا ستتعلم في «DEBUG وSECRET_KEY وALLOWED_HOSTS»؟

تهيئة الإعدادات الحساسة بطريقة صحيحة تتمرن على Django Academy مع أكواد عملية تشغلها مباشرة في المتصفح، ومدرس ذكاء اصطناعي متاح 24/7 يجيب على أسئلتك أثناء عملك.

هل أحتاج إلى خبرة سابقة لأبدأ Django Academy؟

لا تُشترط خبرة سابقة. Django Academy على CoddyKit منظم للمبتدئين حتى المتقدمين، لذا يمكنك البدء من هنا أو من البداية والتقدم بسرعتك الخاصة. هذا هو الدرس 1 من أصل 4.

كم من الوقت يستغرق درس «DEBUG وSECRET_KEY وALLOWED_HOSTS»؟

معظم دروس CoddyKit تستغرق حوالي 5–10 دقائق. كل منها موجز وتفاعلي، لذا تحرز تقدماً مستمراً وتستأنف من حيث توقفت عبر الويب والتطبيق.

هل يمكنني كتابة وتشغيل أكواد في درس Django Academy هذا؟

نعم. كل درس في Django Academy يتضمن محرر أكواد مدمج، لذا تكتب وتشغل أكواداً حقيقية مباشرة في متصفحك وتحصل على تعليقات فورية من الذكاء الاصطناعي — بدون إعداد محلي.

جميع الدروس في هذه الدورة

  1. DEBUG وSECRET_KEY وALLOWED_HOSTS
  2. HTTPS وHSTS وملفات Cookies الآمنة
  3. دفاعات XSS وCSRF وSQL Injection
  4. تنفيذ قائمة التحقق من النشر
← العودة إلى Django Academy