TEMPLATE_DIRS setting in settings.py?The TEMPLATE_DIRS setting tells Django where to find template files on your computer. It is a list of folder paths. Django searches these folders when rendering templates.
DIRS list inside the TEMPLATES setting in settings.py, what will happen when you try to render a template located there?If the directory containing your template is not listed in the DIRS setting, Django will not find it and will raise a TemplateDoesNotExist error.
templates located at the project base directory in Django 4+ settings.py?In Django 4+, BASE_DIR is a Path object from pathlib. To join paths, use the division operator /. The DIRS setting expects a list of paths.
settings.py snippet:TEMPLATES = [{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': ['templates'],
'APP_DIRS': True,
'OPTIONS': {},
}]Your template is located at
/home/user/myproject/templates/index.html. When rendering, Django raises TemplateDoesNotExist. What is the most likely cause?DIRS matches the actual folder location.The DIRS list should contain absolute paths or paths relative to BASE_DIR. Using just 'templates' is relative to the current working directory, which may not be correct.
settings.py snippet:BASE_DIR = Path(__file__).resolve().parent.parent
TEMPLATES = [{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [BASE_DIR / 'custom_templates'],
'APP_DIRS': True,
'OPTIONS': {},
}]And you have 3 Django apps each with a
templates folder inside their app directory. How many directories will Django search for templates when rendering?APP_DIRS does in Django template settings.When APP_DIRS is True, Django automatically looks inside each app's templates folder. It also looks inside the directories listed in DIRS. So total directories searched = 1 (custom_templates) + 3 (app templates) = 4.