Django Tutorial
Django Create A Complete Project
Django Template
Django Database Query
Django Form
Django Authentication and Permission Management
Django unittest
Django Advanced
The settings.py
file is an important component of a Django project, as it contains configuration settings that affect the behavior and functionality of your application. In this tutorial, we will discuss key settings and how to modify them.
Open your Django project's settings.py
file, located in the project's main directory (e.g., myproject/settings.py
).
Debug mode: The DEBUG
setting controls whether your application is running in debug mode. When DEBUG = True
, Django will provide detailed error pages and will not cache templates or static files. In a production environment, set DEBUG = False
to improve performance and security.
DEBUG = True
ALLOWED_HOSTS
setting specifies a list of valid hostnames for your application. In a production environment, you should add the domain names or IP addresses of your server(s).ALLOWED_HOSTS = ['example.com', 'www.example.com', '192.168.1.1']
MIDDLEWARE
setting is a list of middleware classes that process requests and responses in your application. Middleware classes are executed in the order they are listed.MIDDLEWARE = [ 'django.middleware.security.SecurityMiddleware', 'django.contrib.sessions.middleware.SessionMiddleware', 'django.middleware.common.CommonMiddleware', 'django.middleware.csrf.CsrfViewMiddleware', 'django.contrib.auth.middleware.AuthenticationMiddleware', 'django.contrib.messages.middleware.MessageMiddleware', 'django.middleware.clickjacking.XContentOptionsMiddleware', ]
DATABASES
setting configures the database connections for your application. By default, Django uses the SQLite database. To use a different database (e.g., PostgreSQL, MySQL, or Oracle), update the ENGINE
and other required settings.DATABASES = { 'default': { 'ENGINE': 'django.db.backends.postgresql', 'NAME': 'mydatabase', 'USER': 'myuser', 'PASSWORD': 'mypassword', 'HOST': 'localhost', 'PORT': '', } }
TIME_ZONE
setting specifies the default time zone for your application. Set this to the appropriate time zone for your project.TIME_ZONE = 'UTC'
INSTALLED_APPS
setting is a list of Django applications that are enabled for your project. These applications provide functionality such as authentication, the admin interface, and session management. Add your custom applications to this list to enable them.INSTALLED_APPS = [ 'django.contrib.admin', 'django.contrib.auth', 'django.contrib.contenttypes', 'django.contrib.sessions', 'django.contrib.messages', 'django.contrib.staticfiles', 'myapp', ]
STATIC_URL
setting specifies the base URL for serving static files, such as CSS, JavaScript, and images. The STATICFILES_DIRS
setting is a list of directories where Django looks for static files. In a production environment, use the STATIC_ROOT
setting to specify a directory where the collectstatic
management command will gather all static files.STATIC_URL = '/static/' STATICFILES_DIRS = [os.path.join(BASE_DIR, 'static')] STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles')
Django settings.py
File Explained:
settings.py
file in Django contains configuration settings for the entire project. It covers database settings, middleware, security, and more.settings.py
file structure:# settings.py DEBUG = True DATABASES = {...} INSTALLED_APPS = [...]
Django settings.py
Environment Variables:
# settings.py import os SECRET_KEY = os.environ.get('DJANGO_SECRET_KEY')
Django settings.py
Database Configuration:
settings.py
, specifying the database engine, name, user, and password.# settings.py DATABASES = { 'default': { 'ENGINE': 'django.db.backends.postgresql', 'NAME': 'mydatabase', 'USER': 'mydatabaseuser', 'PASSWORD': 'mypassword', 'HOST': 'localhost', 'PORT': '5432', } }
Django settings.py
DEBUG
Mode:
DEBUG
setting in settings.py
controls whether the application is in debugging mode, influencing error handling and display.DEBUG
setting based on the environment:# settings.py DEBUG = os.environ.get('DJANGO_DEBUG', 'False') == 'True'
Django settings.py
Template Configuration:
settings.py
, such as template engines, directories, and context processors.# settings.py TEMPLATES = [ { 'BACKEND': 'django.template.backends.django.DjangoTemplates', 'DIRS': [os.path.join(BASE_DIR, 'templates')], 'APP_DIRS': True, 'OPTIONS': { 'context_processors': [ 'django.template.context_processors.debug', 'django.template.context_processors.request', 'django.contrib.auth.context_processors.auth', 'django.contrib.messages.context_processors.messages', ], }, }, ]
Django settings.py
Middleware Configuration:
settings.py
define the order and behavior of middleware components.# settings.py MIDDLEWARE = [ 'myapp.middleware.CustomMiddleware', # ... ]
Django settings.py
Static Files Configuration:
settings.py
to specify the location for static files like CSS, JavaScript, and images.# settings.py STATIC_URL = '/static/' STATICFILES_DIRS = [os.path.join(BASE_DIR, 'static')]
Django settings.py
Logging Configuration:
settings.py
control how the application logs messages, errors, and warnings.# settings.py LOGGING = { 'version': 1, 'disable_existing_loggers': False, 'handlers': { 'file': { 'level': 'DEBUG', 'class': 'logging.FileHandler', 'filename': '/path/to/django/debug.log', }, }, 'loggers': { 'django': { 'handlers': ['file'], 'level': 'DEBUG', 'propagate': True, }, }, }
Django settings.py
Secret Key:
SECRET_KEY
setting in settings.py
is a cryptographic key used for security purposes.SECRET_KEY
:# settings.py SECRET_KEY = 'mysecretkey'
Django settings.py
Custom Configuration Variables:
settings.py
for project-specific settings.# settings.py MY_CUSTOM_SETTING = 'myvalue'