.env से Environment Variables
In this page:
# .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.
# .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.
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 से पूरी तरह बाहर रखता है।
उदाहरण: 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.
# 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. #}
- सिर्फ एक
.env.exampletemplate commit करने के बजाय एक real.envfile (real secrets के साथ) version control में commit करना। SECRET_KEYको environment से पढ़ने के बजाय directlysettings.pyमें hardcode करना।.envको.gitignoreमें add करना भूल जाना, repo push होने के पल secrets leak करते हुए।
- Environment variables
SECRET_KEYऔर database passwords जैसे secrets को codebase से बाहर रखते हैं। - एक
.envfile इन values को locally simpleKEY=valuelines के रूप में store करती है और startup पर load होती है। settings.pyvalues को hardcoding के बजायos.environ.get()से पढ़ता है।.envको version control से exclude किया जाना चाहिए ताकि real secrets कभी commit न हों।
Chapter Quiz — Complete all 9 topics to unlock
0/9 topics done
Complete these topics first: