×
   ❮   
PYTHON FOR DJANGO DJANGO FOR BEGINNERS DJANGO SPECIFICS PAYMENT INTEGRATION API BASICS Roadmap
     ❯   

PROJECT STRUCTURE

Settings.py

SETTINGS.PY

The settings.py file is a crucial part of a Django project, defining various configurations that dictate how the Django application operates. It contains settings for security, database connections, middleware, and more. Below is a breakdown of the main settings you will encounter in a settings.py file:

Core Settings

DEBUG

DEBUG is a boolean setting that determines whether Django is in debug mode. When set to True, Django will display detailed error pages and log extensive debugging information.

 DEBUG = True 

ALLOWED_HOSTS

ALLOWED_HOSTS is a list of strings representing the host/domain names that this Django site can serve. It helps prevent HTTP Host header attacks.

 
ALLOWED_HOSTS = ['localhost', '127.0.0.1']
 

DATABASES

The DATABASES setting defines the database configuration. It includes the database engine, name, user credentials, and host.

 
DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': 'mydatabase',
        'USER': 'myuser', 
        'PASSWORD': 'mypassword', 
        'HOST': 'localhost', 
        'PORT': '5432',
    }
}

SECRET_KEY

SECRET_KEY is a string used for cryptographic signing. It is crucial for maintaining the security of sessions and other sensitive data.

 
SECRET_KEY = 'your-secret-key' 

INSTALLED_APPS

INSTALLED_APPS is a list of all Django applications that are activated in this Django instance. It includes both default and custom apps.

 
INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',

    #my apps
    'myapp',
]

MIDDLEWARE

MIDDLEWARE is a list of middleware components that are processed during the request/response lifecycle. Middleware can modify requests, responses, and handle exceptions.

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

TEMPLATES

TEMPLATES is a list of configurations for the template engines used to render HTML. It includes settings for loaders, context processors, and more.

 
TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': ['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',
                'courses.processors.course_processor',
            ],
        },
    },
]

STATIC_URL

STATIC_URL defines the URL prefix for serving static files (like CSS and JavaScript) during development.

 STATIC_URL = '/static/' 

MEDIA_URL

MEDIA_URL is the URL prefix for serving media files uploaded by users.

 MEDIA_URL = '/media/' 

BEST PRACTICES

Environment-Specific Settings

Use environment variables or separate settings files for different environments (e.g., development, production) to keep your configuration manageable and secure.

Security

Keep sensitive data like SECRET_KEY and database credentials out of version control. Use environment variables or secure storage solutions to manage these secrets.

Customization

Tailor settings.py to fit the specific needs of your project. This might include adding or modifying settings related to caching, logging, or internationalization.

 

References


Django-tutorial.dev is dedicated to providing beginner-friendly tutorials on Django development. Examples are simplified to enhance readability and ease of learning. Tutorials, references, and examples are continuously reviewed to ensure accuracy, but we cannot guarantee complete correctness of all content. By using Django-tutorial.dev, you agree to have read and accepted our terms of use , cookie policy and privacy policy.

© 2024 Nischal Lamichhane. All Rights Reserved.
Django-tutorial.dev is styled using Bootstrap 5.
And W3.CSS.