In a Django project, template configuration is essential for defining how HTML and other templates are rendered, including the location of template files. The TEMPLATES
setting in the settings.py
file is used to configure template-related settings.
The default TEMPLATES
settings would look like this,
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [],
'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',
],
},
},
]
Here, you can specifiy the path of the templates
folder in the DIRS
key.
For example, if we create a templates
folder in the base directory of our project, then we will need to edit the DIRS
as follows :
import os
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',
],
},
},
]
This will allow, the templates folder in the base directory to be accessed from anywhere in the project.