Deployment की तैयारी करना
# 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 होना चाहिए।
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.
# 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 में इकट्ठी करनी होती हैं।
उदाहरण: 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.
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.
python manage.py check --deploy
⚠️ Run this in your own terminal or Node.js environment.
DEBUG = Trueके साथ deploy करना, जो उस किसी को भी stack traces, settings, और file paths leak करता है जो एक error trigger करे।- Production में real domain list करने के बजाय
ALLOWED_HOSTSको empty या['*']के रूप में छोड़ना। collectstaticrun करना भूल जाना, इसलिए 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 से आनी चाहिए।
Chapter Quiz — Complete all 9 topics to unlock
0/9 topics done
Complete these topics first: