TemplateDoesNotExist at /app_name/
If you are facing above template error, there is a high possibility that at-least one of the below listed point applies to you.
Make sure when rendering the template, you are using right name of template.
return render(request, 'app_name/tools.html', data)
Check if template name is actually tools
or it is tool
?
In the above render statement, confirm if the appname used is correct or not.
return render(request, 'app_name/tools.html', data)
Please confirm if your app is listed in installed apps in settings.py
file.
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'your-app-here',
]
Make sure your templates are places inside a directory with same name as your app's name which in itself is inside templates directory. Confused?
So your templates directory structure should be as below.
app_name | |-templates | |-app_name | | |-tools.html
Confirm if Django is looking for templates in app directories. For this you need to see templates setting in settings.py
file. Check if APP_DIR
is set to True
. If this is set to False
, Django won't look for templates in app directories.
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',
],
},
},
]
If you are using something like below in your project's urls.py
file
urlpatterns += [
path(r'robots.txt/', TemplateView.as_view(template_name="project_name/robots.txt", content_type='text/plain')),
]
It could be any file instead of robots.txt
, then make sure there is templates directory in root directory of project. This template directory is outside of every app. Directory structure would be as below.
|-project_name | |-templates | | |-project_name | | | |-your-template-here
These template files are project level template files.
Also add 'DIRS': [os.path.join(BASE_DIR, 'templates')],
in TEMPLATES
tuple in settings.py
file. BASE_DIR
is defined in settings.py
file at the top. If not define as below.
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
If you are still unable to resolve the error, feel free to comment or contact us or connect on facebook page.