← Back to Django Course | Chapter 5: Migrations & Database Management | Lesson 8 of 8

settings.py में Database Configuration

settings.py में DATABASES dictionary Django को बताती है कि किस database से बात करनी है और इससे कैसे connect करना है।
Syntax
markup
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.

markup
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.

markup
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.

bash
python manage.py shell
>>> from django.conf import settings
>>> settings.DATABASES['default']['ENGINE']

⚠️ 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. Real database passwords settings.py के अंदर directly hard-code करना और उस file को version control में commit करना।
  2. यह भूल जाना कि DATABASES setting को एक अलग database में change करने से existing data move नहीं होता — यह बस Django को कहीं नए और empty जगह point करता है।
  3. 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 नहीं।

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.