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

.env से Environment Variables

Environment variables उन sticky notes जैसे हैं जो आप अपने code के बाहर रखते हैं -- ताकि आपके secret passwords कभी उस book में print न हों जो हर कोई पढ़ सकता है (आपका source code)।
Syntax
markup
# .env
KEY_NAME=value

# settings.py
from dotenv import load_dotenv
import os

load_dotenv()
KEY_NAME = os.environ.get('KEY_NAME')

एक .env File का Shape

एक .env file एक plain text file है जिसमें हर line पर एक KEY=value pair होता है। यह project root में रहती है, कभी version control में commit नहीं होती, और developers और environments के बीच अलग होने वाली values रखती है।

उदाहरण: The Shape of a .env File

A .env file is a plain text file with one KEY=value pair per line. It lives in the project root, is never committed to version control, and holds values that differ between developers and environments.

markup
# .env (example values only -- never commit real secrets)
SECRET_KEY=your-secret-key-here
DJANGO_DEBUG=True
DATABASE_NAME=db.sqlite3
{# 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. #}

python-dotenv के साथ .env Values Load करना

python-dotenv package एक .env file पढ़ता है और इसकी values environment में load करता है ताकि settings.py इन्हें os.environ.get() से pick up कर सके, exactly वैसे ही जैसे यह production में hosting platform द्वारा set की गई variables पढ़ता।

उदाहरण: Loading .env Values with python-dotenv

The python-dotenv package reads a .env file and loads its values into the environment so settings.py can pick them up with os.environ.get(), the same way it would read variables set by the hosting platform in production.

bash
pip install python-dotenv

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

settings.py में Values पढ़ना

Load हो जाने के बाद, environment variables os.environ.get() से पढ़े जाते हैं, local development के लिए एक sensible default के साथ। यह real secret value को source code से पूरी तरह बाहर रखता है।

Warning: ऐसा fallback कभी hardcode न करें जो एक real production secret जैसा दिखे -- defaults को सिर्फ local use के लिए obviously safe रखें।

उदाहरण: Reading Values in settings.py

Once loaded, environment variables are read with os.environ.get(), with a sensible default for local development. This keeps the real secret value out of the source code entirely.

markup
# settings.py
import os
from dotenv import load_dotenv

load_dotenv()

SECRET_KEY = os.environ.get('SECRET_KEY', 'dev-only-insecure-key')
DEBUG = os.environ.get('DJANGO_DEBUG', 'True') == 'True'
{# 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. #}
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. सिर्फ एक .env.example template commit करने के बजाय एक real .env file (real secrets के साथ) version control में commit करना।
  2. SECRET_KEY को environment से पढ़ने के बजाय directly settings.py में hardcode करना।
  3. .env को .gitignore में add करना भूल जाना, repo push होने के पल secrets leak करते हुए।
चैप्टर सारांश
  • Environment variables SECRET_KEY और database passwords जैसे secrets को codebase से बाहर रखते हैं।
  • एक .env file इन values को locally simple KEY=value lines के रूप में store करती है और startup पर load होती है।
  • settings.py values को hardcoding के बजाय os.environ.get() से पढ़ता है।
  • .env को version control से exclude किया जाना चाहिए ताकि real secrets कभी commit न हों।

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.