← Back to Django Course | Chapter 15: Security, Caching & Deployment | Lesson 6 of 9

Deployment की तैयारी करना

Deployment के लिए prepare करना एक trip से पहले suitcase pack करने जैसा है -- आप एक list check off करते हैं ताकि आपकी site real visitors के लिए live होने से पहले कुछ important पीछे न छूटे।
Syntax
markup
# settings.py (production)
DEBUG = False
ALLOWED_HOSTS = ['example.com']
STATIC_ROOT = BASE_DIR / 'staticfiles'

python manage.py collectstatic

DEBUG को बंद करना

Django के debug pages development के दौरान बेहद helpful होते हैं क्योंकि वे पूरे stack traces, settings values, और SQL queries दिखाते हैं। Production में, वही information किसी stranger को दिया जाना एक serious security risk है, इसलिए DEBUG False होना चाहिए।

Warning: DEBUG = True के साथ कभी deploy न करें -- यह आपकी settings और code उस किसी को भी expose करता है जो एक error page trigger कर सके।

उदाहरण: Turning Off DEBUG

Django's debug pages are extremely helpful during development because they show full stack traces, settings values, and SQL queries. In production, that same information handed to a stranger is a serious security risk, so DEBUG must be False.

markup
# settings.py
import os

DEBUG = os.environ.get('DJANGO_DEBUG', 'False') == 'True'
ALLOWED_HOSTS = ['www.example.com', 'example.com']
{# Django-only code -- models.py/views.py/urls.py/settings.py snippets, or template markup using Django template tags/variables -- can't run standalone via Judge0 or the browser preview, since it needs a real Django project. Only this course's pure-Python examples (example_lang == 'python', no Django imports) are actually runnable, so those still get the button below. #}

Static Files Collect करना

Development के दौरान, Django convenience के लिए static files खुद serve करता है। Production में, एक dedicated web server (या cloud storage) इसके बजाय इन्हें serve करता है, इसलिए सारी static files पहले collectstatic के साथ एक directory में इकट्ठी करनी होती हैं।

Note: Redeploy करने से पहले static files बदलने पर हर बार collectstatic run करें।

उदाहरण: Collecting Static Files

During development, Django serves static files itself for convenience. In production, a dedicated web server (or cloud storage) serves them instead, so all static files first need to be gathered into one directory with collectstatic.

bash
python manage.py collectstatic --noinput

⚠️ Run this in your own terminal or Node.js environment.

Deployment Readiness जांचना

Django production settings के लिए specifically एक built-in check ship करता है। Deploy करने से पहले इसे run करना एक weak SECRET_KEY या missing security headers जैसी common misconfigurations पकड़ता है।

उदाहरण: Checking Deployment Readiness

Django ships a built-in check specifically for production settings. Running it before deploying catches common misconfigurations like a weak SECRET_KEY or missing security headers.

bash
python manage.py check --deploy

⚠️ Run this in your own terminal or Node.js environment.

Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. DEBUG = True के साथ deploy करना, जो उस किसी को भी stack traces, settings, और file paths leak करता है जो एक error trigger करे।
  2. Production में real domain list करने के बजाय ALLOWED_HOSTS को empty या ['*'] के रूप में छोड़ना।
  3. collectstatic run करना भूल जाना, इसलिए CSS/JS files live site पर 404 होती हैं भले ही वे locally काम करती थीं।
चैप्टर सारांश
  • Deploy करने से पहले DEBUG को False पर set किया जाना चाहिए, क्योंकि debug pages sensitive internal details expose करते हैं।
  • ALLOWED_HOSTS को exact domain(s) list करना चाहिए जिनसे site serve होगी।
  • Static files को collectstatic के साथ एक folder में collect करना होता है ताकि एक web server इन्हें serve कर सके।
  • SECRET_KEY और database passwords जैसे secrets hardcoded values के बजाय environment variables से आनी चाहिए।

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.