Lesson 12 of 60 – Django Settings
20%

⚙️ Django Settings

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.

Note: The 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.

1. What Is settings.py?

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.

2. Location of settings.py

The file is normally located inside the inner project package.

myproject/
    manage.py
    myproject/
        __init__.py
        settings.py
        urls.py
        asgi.py
        wsgi.py

3. Why settings.py Is Important

The settings file controls many important parts of a Django application.

  • Installed applications
  • Database configuration
  • Middleware
  • Templates
  • Static files
  • Language and time zone
  • Security settings

4. BASE_DIR

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.

5. SECRET_KEY

Django creates a SECRET_KEY setting for cryptographic signing and other security-related operations.

SECRET_KEY = 'your-secret-key'
Important: Keep the secret key private and do not expose it unnecessarily in public code repositories.

6. DEBUG

The DEBUG setting controls Django's debug mode.

DEBUG = True

During development, debug mode can provide detailed information when an error occurs.

7. DEBUG in Production

Debug mode should be configured carefully for a production deployment.

Detailed debug information can expose information that should not be publicly visible.

DEBUG = False

8. ALLOWED_HOSTS

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.

9. INSTALLED_APPS

INSTALLED_APPS contains the applications enabled in the Django project.

INSTALLED_APPS = [
    ...
]

Django includes several built-in applications in a newly created project.

10. Django Built-in Apps

A new Django project commonly includes built-in applications such as:

  • admin
  • auth
  • contenttypes
  • sessions
  • messages
  • staticfiles

These applications provide commonly needed functionality.

11. Add Your Own App

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.

12. MIDDLEWARE

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.

13. ROOT_URLCONF

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.

14. TEMPLATES

The TEMPLATES setting configures Django's template system.

Templates are commonly used to generate HTML pages dynamically.

TEMPLATES = [
    {
        ...
    },
]

15. WSGI_APPLICATION

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.

16. ASGI Configuration

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.

17. DATABASES

The DATABASES setting defines the database configuration used by Django.

A newly created project includes a default database configuration.

DATABASES = {
    'default': {
        ...
    }
}

18. SQLite Database

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',
    }
}

19. LANGUAGE_CODE

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.

20. TIME_ZONE

The TIME_ZONE setting defines the project's default time zone.

TIME_ZONE = 'UTC'

This can be changed according to the application's requirements.

21. USE_I18N

USE_I18N controls whether Django's internationalization system is enabled.

USE_I18N = True

Internationalization is useful when applications need to support multiple languages.

22. USE_TZ

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.

23. Static Files

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.

24. Media Files

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.

25. Templates Directory

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'],

26. Security Settings

Django provides several settings related to security.

Examples include:

  • SECRET_KEY
  • DEBUG
  • ALLOWED_HOSTS
  • CSRF-related configuration
  • Session-related configuration

Security settings should be configured carefully, especially for production.

27. Organizing Settings

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.

28. Example Important Settings

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.

29. Be Careful When Editing settings.py

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.

30. Django Settings Summary

The settings.py file is the central configuration file of a Django project.

Important settings include:

  • SECRET_KEY
  • DEBUG
  • ALLOWED_HOSTS
  • INSTALLED_APPS
  • MIDDLEWARE
  • DATABASES
  • TEMPLATES
  • LANGUAGE_CODE
  • TIME_ZONE
  • STATIC_URL

Understanding these settings will help you configure Django applications correctly as you progress through the course.

📌 Key Points

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

🧠 Quick Quiz

Question: Which Django file contains the main project configuration?