Solving Django Error: TemplateDoesNotExist at /app_name/


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.



1. Wrong template name:

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?



2. Wrong app name:

In the above render statement, confirm if the appname used is correct or not.

return render(request, 'app_name/tools.html', data)



3. Installed Apps:

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



4. Template Directory Structure:

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



5. Where is Django looking for templates:

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',
],
},
},
]



6. Project level templates:

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.

Host your Django project for free.


Related Articles:
Designing custom 404 and 500 error pages in Django
How to create your own awesome 404 error or 500 error page in django. Step by step guide to help you design your own 404 not found error page in Django. Design your own 500 internal server error page in Django application...
How to use AJAX with Django
How to use AJAX in Django projects?, Checking username availability without submitting form, Making AJAX calls from Django code, loading data without refreshig page in django templates, AJAX and Django,...
Logging databases changes in Django Application
Logging databases changes in Django Application, Using Django signals to log the changes made to models, Model auditing using Django signals, Creating signals to track and log changes made to database tables, tracking model changes in Django, Change in table Django, Database auditing Django, tracking Django database table editing...
Displaying custom 404 error (page not found) page in Django 2.0
How to create custom 404 error page in Django. Handler404 example Django 2.0. Page not found error in Django 2.0. Custom Error handlers in Django 2.0...

1 thought on 'Solving Django Error: Templatedoesnotexist At /App_Name/'
Lary :
This was very helpful. Thanks a lot.

Leave a comment:


*All Fields are mandatory. **Email Id will not be published publicly.


SUBSCRIBE
Please subscribe to get the latest articles in your mailbox.



Recent Posts:






© pythoncircle.com 2018-2019
Contact Us: code108labs [at] gmail.com
Address: 3747 Smithfield Avenue, Houston, Texas