0
0
Djangoframework~5 mins

Template configuration and directories in Django

Choose your learning style9 modes available
Introduction

Templates help you create the HTML pages your website shows. Configuring templates tells Django where to find these HTML files.

When you want to separate your website design (HTML) from your Python code.
When you need to organize your HTML files in folders for easy management.
When you want to reuse the same layout for many pages.
When you want Django to automatically find your HTML templates.
When you add new apps and want their templates to be found.
Syntax
Django
TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [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',
            ],
        },
    },
]

DIRS is a list of folders where Django looks for templates outside apps.

APP_DIRS=True tells Django to look inside each app's templates folder automatically.

Examples
Django will only look inside app folders for templates.
Django
TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [],  # No extra folders
        'APP_DIRS': True,
        'OPTIONS': {},
    },
]
Django looks only in the custom_templates folder, not inside apps.
Django
TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [BASE_DIR / 'custom_templates'],
        'APP_DIRS': False,
        'OPTIONS': {},
    },
]
Django looks in multiple folders and inside apps for templates.
Django
TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [BASE_DIR / 'templates', BASE_DIR / 'extra_templates'],
        'APP_DIRS': True,
        'OPTIONS': {},
    },
]
Sample Program

This example shows how to set up the template folders and print them out. It uses BASE_DIR to find the main project folder and adds a templates folder there. It also enables looking inside app folders.

Django
from pathlib import Path

BASE_DIR = Path(__file__).resolve().parent.parent

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

print("Template directories:")
for d in TEMPLATES[0]['DIRS']:
    print(d)
print("App directories enabled:", TEMPLATES[0]['APP_DIRS'])
OutputSuccess
Important Notes

Always create a templates folder in your project root or inside each app for Django to find your HTML files.

If APP_DIRS is True, Django looks inside each app's templates folder automatically.

You can add many folders in DIRS if you want to organize templates in different places.

Summary

Templates are HTML files that Django uses to show pages.

Configure TEMPLATES in settings.py to tell Django where to find these files.

Use DIRS for custom folders and APP_DIRS=True to find templates inside apps.