In this lesson, you will learn about the settings.py file in Django. This file contains important configuration options that control how your Django project works.
settings.py file is one of the
most important files in a Django project. It contains project-wide
configuration such as installed apps, database settings, middleware,
templates, static files and security-related settings.
settings.py is the main configuration file of a Django project.
Django reads the settings defined in this file when starting and running the application.
The file is normally located inside the inner project package.
myproject/
manage.py
myproject/
__init__.py
settings.py
urls.py
asgi.py
wsgi.py
The settings file controls many important parts of a Django application.
A new Django project normally defines a BASE_DIR variable.
BASE_DIR = Path(__file__).resolve().parent.parent
It represents the base directory of the Django project and can be used when constructing paths to files and directories.
Django creates a SECRET_KEY setting for cryptographic
signing and other security-related operations.
SECRET_KEY = 'your-secret-key'
The DEBUG setting controls Django's debug mode.
DEBUG = True
During development, debug mode can provide detailed information when an error occurs.
Debug mode should be configured carefully for a production deployment.
Detailed debug information can expose information that should not be publicly visible.
DEBUG = False
ALLOWED_HOSTS defines the host/domain names that Django
can serve when host validation is enabled.
ALLOWED_HOSTS = []
This setting is commonly configured when preparing a Django application for deployment.
INSTALLED_APPS contains the applications enabled in the
Django project.
INSTALLED_APPS = [
...
]
Django includes several built-in applications in a newly created project.
A new Django project commonly includes built-in applications such as:
These applications provide commonly needed functionality.
When you create your own Django app, it can be added to
INSTALLED_APPS.
For example:
INSTALLED_APPS = [
...
'students',
]
The exact app name depends on the application you create.
The MIDDLEWARE setting contains middleware components
that process requests and responses.
MIDDLEWARE = [
...
]
Middleware can perform processing before a request reaches a view and after a view produces a response.
The ROOT_URLCONF setting identifies the project's main
URL configuration module.
ROOT_URLCONF = 'myproject.urls'
Django uses this configuration to find the main URL patterns.
The TEMPLATES setting configures Django's template system.
Templates are commonly used to generate HTML pages dynamically.
TEMPLATES = [
{
...
},
]
The WSGI_APPLICATION setting identifies the WSGI application
used by the project.
WSGI_APPLICATION = 'myproject.wsgi.application'
It is relevant when running Django through a WSGI-compatible server.
Django projects also include an ASGI configuration file.
ASGI_APPLICATION = 'myproject.asgi.application'
ASGI provides an interface for asynchronous-capable Python web servers and applications.
The DATABASES setting defines the database configuration
used by Django.
A newly created project includes a default database configuration.
DATABASES = {
'default': {
...
}
}
New Django projects commonly use SQLite as the default database.
SQLite is convenient for learning and many development scenarios because it stores the database in a file.
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': BASE_DIR / 'db.sqlite3',
}
}
The LANGUAGE_CODE setting defines the language used by
Django's internationalization system.
LANGUAGE_CODE = 'en-us'
You can configure the language according to your application's requirements.
The TIME_ZONE setting defines the project's default time zone.
TIME_ZONE = 'UTC'
This can be changed according to the application's requirements.
USE_I18N controls whether Django's internationalization
system is enabled.
USE_I18N = True
Internationalization is useful when applications need to support multiple languages.
The USE_TZ setting controls whether Django uses
time zone-aware date and time handling.
USE_TZ = True
Time zone handling becomes important when applications work with users or data from different regions.
Django provides settings for managing static files such as CSS, JavaScript and images.
A common setting is:
STATIC_URL = 'static/'
More static file configuration can be added as a project grows.
Applications that allow users to upload files may require additional media configuration.
Common settings include:
MEDIA_URL = 'media/'
MEDIA_ROOT = BASE_DIR / 'media'
These settings are used for user-uploaded media files.
You can configure Django to find templates in project-level directories.
A common approach is to use the DIRS option in the
TEMPLATES setting.
'DIRS': [BASE_DIR / 'templates'],
Django provides several settings related to security.
Examples include:
Security settings should be configured carefully, especially for production.
As a Django project becomes larger, settings can become more complex.
Developers may organize settings into separate development, testing and production configurations. The exact structure depends on the project.
BASE_DIR = Path(__file__).resolve().parent.parent
SECRET_KEY = 'your-secret-key'
DEBUG = True
ALLOWED_HOSTS = []
INSTALLED_APPS = [
...
]
DATABASES = {
'default': {
...
}
}
LANGUAGE_CODE = 'en-us'
TIME_ZONE = 'UTC'
STATIC_URL = 'static/'
These are examples of common settings found in a Django project.
Incorrect settings can prevent a Django project from starting or can cause unexpected behavior.
Make changes carefully and test the project after modifying important configuration options.
The settings.py file is the central configuration file
of a Django project.
Important settings include:
SECRET_KEYDEBUGALLOWED_HOSTSINSTALLED_APPSMIDDLEWAREDATABASESTEMPLATESLANGUAGE_CODETIME_ZONESTATIC_URLUnderstanding these settings will help you configure Django applications correctly as you progress through the course.
settings.py contains the main Django project configuration.INSTALLED_APPS contains enabled Django applications.DATABASES defines database configuration.MIDDLEWARE contains request and response processing components.TEMPLATES configures Django's template system.DEBUG controls debug mode.ALLOWED_HOSTS controls accepted host/domain names.STATIC_URL is used for static file configuration.LANGUAGE_CODE and TIME_ZONE control language and time settings.Question: Which Django file contains the main project configuration?