settings.py में Database Configuration
In this page:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.database_backend',
'NAME': 'database_name',
'USER': 'username',
'PASSWORD': 'password',
'HOST': 'host',
'PORT': 'port',
}
}
Default SQLite Setting
एक नए Django project की settings.py SQLite के लिए पहले से configured एक DATABASES dictionary के साथ आती है, sqlite3 backend और BASE_DIR के नीचे एक file path उपयोग करते हुए।
उदाहरण: The Default SQLite Setting
default is the connection Django uses for every query unless a view explicitly asks for a different one — this is that exact out-of-the-box setting.
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': BASE_DIR / '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. #}
Database Connection के लिए Required Keys
DATABASES में हर entry को कम से कम यह जानने के लिए एक ENGINE (Django को कौन सा backend उपयोग करना चाहिए) और एक NAME (database name, या SQLite के लिए file path) चाहिए कि कहां और कैसे connect करना है।
उदाहरण: Required Keys for a Database Connection
ENGINE tells Django which backend module to load; NAME tells it exactly which file (for SQLite) holds the data.
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': BASE_DIR / '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. #}
Active Connection Check करना
आप सीधे shell से confirm कर सकते हैं कि Django currently किस database को उपयोग करने के लिए configured है, जो settings changes debug करते समय उपयोगी है।
उदाहरण: Checking the Active Connection
This prints the ENGINE string currently active, e.g. 'django.db.backends.sqlite3', confirming exactly which backend settings.py is pointing at.
python manage.py shell
>>> from django.conf import settings
>>> settings.DATABASES['default']['ENGINE']
⚠️ Run this in your own terminal or Node.js environment.
- Real database passwords settings.py के अंदर directly hard-code करना और उस file को version control में commit करना।
- यह भूल जाना कि DATABASES setting को एक अलग database में change करने से existing data move नहीं होता — यह बस Django को कहीं नए और empty जगह point करता है।
- default dictionary के अंदर एक key (जैसे NAME बनाम DBNAME) गलत spell करना, जिससे Django startup पर तुरंत fail हो जाता है।
- DATABASES named connections की एक dictionary है, और default वह है जिसे Django उपयोग करता है जब तक और न कहा जाए।
- हर connection को एक ENGINE (कौन सा database backend) और NAME जैसे connection details चाहिए।
- SQLite को सिर्फ एक NAME (एक file path) चाहिए; दूसरी databases को HOST, PORT, USER, और PASSWORD भी चाहिए।
- Passwords जैसी sensitive values environment variables में रहनी चाहिए, settings.py में hard-coded नहीं।
Chapter Quiz — Complete all 8 topics to unlock
0/8 topics done
Complete these topics first: