[Fixed]-Django & the "TemplateDoesNotExist" error

9đź‘Ť

âś…

Holy mother of god! I solved it!

I do not know why – but this is the solution to the “TemplateDoesNotExist” error (in my case).

My folder structure is like this:

netz2 > skateproject

till now i had the templates folder in skateproject and in settings.py i pointed to this directory.
this threw the template does not exist error when I tried to open the page in firefox.

as skateproject is the project folder in there ive got an folder sk8 – which is the app that im currently working on and that im trying to execute.
The solution is super simple.

I had to move the templates in the subdirectory of the app. which looks like this

netz2 > skateproject > sk8 > templates

and now it works!

So if you have the same problem, make sure your templates folder is not in the root of the project but is a subdirectory of the app youre working on – AND add this path to the settings.py Template_dirs

it looks like this in my example:

TEMPLATE_DIRS = (
    r'H:/netz2/skateprojekt/sk8/templates/',
)
👤Daniel Prell

15đź‘Ť

One of the solution to this problem is you should add apps to in settings.py. I assume you have an application named such as invoice then solution to this is

INSTALLED_APPS = [
'invoice.apps.InoviceConfig',
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
]
👤Harun ERGUL

9đź‘Ť

If anyone is trying this with Django 1.7 and Python 3 or higher, I did this:

TEMPLATE_DIRS = [os.path.join(BASE_DIR, 'mysite_dir/templates')]

where myfile_dir can be any dir under mysite. File structure would be something like this:

manage.py
mysite/  
  mysite/  
  mysite_dir/  
  templates/  
    ____init__.py  
    models.py  
    #...
  another_app/  

This answer may seem repetitive but I know I always like to see things from a more recent date… so hope this helps

👤Jeremy

3đź‘Ť

I struggled with a similar issue for a good several hours and found that I could get the template to load if I put it in a /templates subdirectory within the app using the template. You don’t even need to set TEMPLATE_DIRS in settings.py, the file is found automatically. Unfortunately the TemplateDoesNotExist exception is not very descriptive and in some cases literally wrong. If you see in the Template-loader postmortem something similar to the following:

Django tried loading these templates, in this order:
Using loader django.template.loaders.filesystem.Loader:
/Users/me/mysite/myapp/index.html

And the file path looks correct for the template you’re trying to load, it’s likely that the file exists but it’s not accessible. Try moving it to a /templates subdirectory of the app.

👤Mike Darmetko

3đź‘Ť

If you follow Django’s documentation, they recommend namespacing your templates, where under templates, you have /yourapp/ again before the templates. See here.

So your structure might look like:

Project Folder
--Project
--yourapp
  --templates
    --yourapp
      --layout.html

But what they don’t make clear is that any hardcoded references to those templates (for example, the render function), need to also prepend your app name to those as well! I thought Django was doing a recursive search for us, but I gave it too much credit. You need to point to the exact link.

e.g.

return render(request, "login.html")

goes to

return render(request, "orders/login.html")

You’ll likely need this too in any extends statements or where you use static to link to files.

👤Joel Wigton

0đź‘Ť

For me {% include 'some.html' %} was throwing this error.
Make sure to include the leading ./ like:
{% include './some.html' %}

👤Shruti

0đź‘Ť

Just paste the path of your templates directory from your source root in your project settings dirs variable. It should look something like this

    'DIRS': [
        'templates'
    ],
👤Aakash Dev

-8đź‘Ť

just set DEBUG to false in your Django settings file

👤user5998907

Leave a comment