Database Configuration in settings.py
In this page:
The Default SQLite Setting
A new Django project's settings.py ships with a DATABASES dictionary already configured for SQLite, using the sqlite3 backend and a file path under BASE_DIR.
Example: 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. #}
Required Keys for a Database Connection
Every entry in DATABASES needs at minimum an ENGINE (the backend Django should use) and a NAME (the database name, or file path for SQLite) to know where and how to connect.
Example: 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. #}
Checking the Active Connection
You can confirm which database Django is currently configured to use directly from the shell, which is useful when debugging settings changes.
Example: 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 command in your terminal.
- Hard-coding real database passwords directly inside settings.py and committing that file to version control.
- Forgetting that changing the DATABASES setting to a different database doesn't move existing data — it just points Django somewhere new and empty.
- Misspelling a key inside the default dictionary (like NAME vs DBNAME), which causes Django to fail immediately on startup.
- DATABASES is a dictionary of named connections, and default is the one Django uses unless told otherwise.
- Each connection needs an ENGINE (which database backend) and connection details like NAME.
- SQLite only needs a NAME (a file path); other databases also need HOST, PORT, USER, and PASSWORD.
- Sensitive values like passwords belong in environment variables, not hard-coded in settings.py.
Chapter Quiz — Complete all 8 topics to unlock
0/8 topics done
Complete these topics first: