[django] Django TemplateDoesNotExist?

My local machine is running Python 2.5 and Nginx on Ubuntu 8.10, with Django builded from latest development trunk.

For every URL I request, it throws:

TemplateDoesNotExist at /appname/path appname/template_name.html

Django tried loading these templates, in this order: * Using loader django.template.loaders.filesystem.function: * Using loader django.template.loaders.app_directories.function:

TEMPLATE_DIRS ('/usr/lib/python2.5/site-packages/projectname/templates',)

Is it looking for /usr/lib/python2.5/site-packages/projectname/templates/appname/template_name.html in this case? The weird thing is this file does existed on disk. Why can't Django locate it?

I run the same application on a remote server with Python 2.6 on Ubuntu 9.04 without such problem. Other settings are the same.

Is there anything misconfigured on my local machine, or what could possibly have caused such errors that I should look into?

In my settings.py, I have specified:

SETTINGS_PATH = os.path.normpath(os.path.dirname(__file__))
# Find templates in the same folder as settings.py.
TEMPLATE_DIRS = (
    os.path.join(SETTINGS_PATH, 'templates'),
)

It should be looking for the following files:

  • /usr/lib/python2.5/site-packages/projectname/templates/appname1/template1.html
  • /usr/lib/python2.5/site-packages/projectname/templates/appname1/template2.html
  • /usr/lib/python2.5/site-packages/projectname/templates/appname2/template3.html
  • ...

All the above files exist on disk.

Solved

It works now after I tried:

chown -R www-data:www-data /usr/lib/python2.5/site-packages/projectname/*

It's strange. I don't need to do this on the remote server to make it work.

This question is related to django

The answer is


I must use templates for a internal APP and it works for me:

'DIRS': [os.path.join(BASE_DIR + '/THE_APP_NAME', 'templates')],

Check that your templates.html are in /usr/lib/python2.5/site-packages/projectname/templates dir.


I came up with this problem. Here is how I solved this:

Look at your settings.py, locate to TEMPLATES variable, inside the TEMPLATES, add your templates path inside the DIRS list. For me, first I set my templates path as TEMPLATES_PATH = os.path.join(BASE_DIR,'templates'), then add TEMPLATES_PATH into DIRS list, 'DIRS':[TEMPLATES_PATH,]. Then restart the server, the TemplateDoesNotExist exception is gone. That's it.


1.create a folder 'templates' in your 'app'(let say you named such your app) and you can put the html file here. But it s strongly recommended to create a folder with same name('app') in 'templates' folder and only then put htmls there. Into the 'app/templates/app' folder

2.now in 'app' 's urls.py put:

  path('', views.index, name='index'), # in case of  use default server index.html 

3. in 'app' 's views.py put:

from django.shortcuts import render 

def index(request): return
    render(request,"app/index.html")
    # name 'index' as you want

Check permissions on templates and appname directories, either with ls -l or try doing an absolute path open() from django.


Just a hunch, but check out this article on Django template loading. In particular, make sure you have django.template.loaders.app_directories.Loader in your TEMPLATE_LOADERS list.


I added this

TEMPLATE_DIRS = (
    os.path.join(SETTINGS_PATH, 'templates'),
)

and it still showed the error, then I realized that in another project the templates was showing without adding that code in settings.py file so I checked that project and I realized that I didn't create a virtual environment in this project so I did

virtualenv env 

and it worked, don't know why


In setting .py remove TEMPLATE_LOADERS and TEMPLATE DIRS Then ADD

TEMPLATES = [
 {
    'BACKEND': 'django.template.backends.django.DjangoTemplates',
    'DIRS': ['/home/jay/apijay/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',
        ],
    },
 },
]

Find this tuple:

    import os
    SETTINGS_PATH = os.path.dirname(os.path.dirname(__file__))

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

You need to add to 'DIRS' the string

"os.path.join(SETTINGS_PATH, 'templates')"

So altogether you need:

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [os.path.join(SETTINGS_PATH, '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 encounter this problem when you add an app from scratch. It is probably because that you miss some settings. Three steps is needed when adding an app.

1?Create the directory and template file.

Suppose you have a project named mysite and you want to add an app named your_app_name. Put your template file under mysite/your_app_name/templates/your_app_name as following.

+-- mysite
¦   +-- settings.py
¦   +-- urls.py
¦   +-- wsgi.py
+-- your_app_name
¦   +-- admin.py
¦   +-- apps.py
¦   +-- models.py
¦   +-- templates
¦   ¦   +-- your_app_name
¦   ¦       +-- my_index.html
¦   +-- urls.py
¦   +-- views.py

2?Add your app to INSTALLED_APPS.

Modify settings.py

INSTALLED_APPS = [
    ...
    'your_app_name',
    ...
]

3?Add your app directory to DIRS in TEMPLATES.

Modify settings.py.

TEMPLATES = [
    {
        ...
        'DIRS': [os.path.join(BASE_DIR, 'templates'),
                 os.path.join(BASE_DIR, 'your_app_name', 'templates', 'your_app_name'),
                ...
                ]
    }
]

I'm embarrassed to admit this, but the problem for me was that a template had been specified as ….hml instead of ….html. Watch out!


in your setting.py file replace DIRS in TEMPLATES array with this

'DIRS': []

to this

'DIRS': [os.path.join(BASE_DIR, 'templates')],

but 1 think u need to know is that you have to make a folder with name templates and it should on the root path otherwise u have to change the DIRS value


Works on Django 3

I found I believe good way, I have the base.html in root folder, and all other html files in App folders, I settings.py

import os

# This settings are to allow store templates,static and media files in root folder

BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
TEMPLATE_DIR = os.path.join(BASE_DIR,'templates')
STATIC_DIR = os.path.join(BASE_DIR,'static')
MEDIA_DIR = os.path.join(BASE_DIR,'media')

# This is default path from Django, must be added 
#AFTER our BASE_DIR otherwise DB will be broken.
BASE_DIR = Path(__file__).resolve().parent.parent

# add your apps to Installed apps
INSTALLED_APPS = [
    'main',
    'weblogin',
     ..........
]

# Now add TEMPLATE_DIR to  'DIRS' where in TEMPLATES like bellow
TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [TEMPLATE_DIR, BASE_DIR,],
        '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',
            ],
        },
    },
]
# On end of Settings.py put this refferences to Static and Media files
STATICFILES_DIRS = [STATIC_DIR,]
STATIC_URL = '/static/'

MEDIA_ROOT = [MEDIA_DIR,]
MEDIA_URL = '/media/'

If you have problem with Database, please check if you put the original BASE_DIR bellow the new BASE_DIR otherwise change

# Original
'NAME': BASE_DIR / 'db.sqlite3',
# to
'NAME': os.path.join(BASE_DIR,'db.sqlite3'),

Django now will be able to find the HTML and Static files both in the App folders and in Root folder without need of adding the name of App folder in front of the file.

Struture:
-DjangoProject
    -static(css,JS ...)
    -templates(base.html, ....)
    -other django files like (manage.py, ....)
    -App1
        -templates(index1.html, other html files can extend now base.html too)
        -other App1 files
    -App2
        -templates(index2.html, other html files can extend now base.html too)
        -other App2 files

I had an embarrassing problem...

I got this error because I was rushing and forgot to put the app in INSTALLED_APPS. You would think Django would raise a more descriptive error.


For the django version 1.9,I added

'DIRS': [os.path.join(BASE_DIR, 'templates')], 

line to the Templates block in settings.py And it worked well


Hi guys I found a new solution. Actually it is defined in another template so instead of defining TEMPLATE_DIRS yourself, put your directory path name at their: enter image description here


It works now after I tried

chown -R www-data:www-data /usr/lib/python2.5/site-packages/projectname/*

It's strange. I dont need to do this on the remote server to make it work.

Also, I have to run the following command on local machine to make all static files accessable but on remote server they are all "root:root".

chown -R www-data:www-data /var/www/projectname/*

Local machine runs on Ubuntu 8.04 desktop edition. Remote server is on Ubuntu 9.04 server edition.

Anybody knows why?


Django TemplateDoesNotExist error means simply that the framework can't find the template file.

To use the template-loading API, you'll need to tell the framework where you store your templates. The place to do this is in your settings file (settings.py) by TEMPLATE_DIRS setting. By default it's an empty tuple, so this setting tells Django's template-loading mechanism where to look for templates.

Pick a directory where you'd like to store your templates and add it to TEMPLATE_DIRS e.g.:

TEMPLATE_DIRS = (
  '/home/django/myproject/templates',
)

Make sure you've added your app to the project-name/app-namme/settings.py INSTALLED_APPS: .

INSTALLED_APPS = ['app-name.apps.AppNameConfig']

And on project-name/app-namme/settings.py TEMPLATES: .

'DIRS': [os.path.join(BASE_DIR, 'templates')],

See which folder django try to load template look at Template-loader postmortem in error page, for example, error will sothing like this:

Template-loader postmortem

Django tried loading these templates, in this order:

Using engine django:
django.template.loaders.filesystem.Loader: d:\projects\vcsrc\vcsrc\templates\base.html (Source does not exist)

In my error vcsrc\vcsrc\templates\base.html not in path.
Then change TEMPLATES in setting.py file to your templates path

TEMPLATES = [
    {    
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
         # 'DIRS': [], 
        'DIRS': [os.path.join(BASE_DIR, 'vcsrc/templates')], 
        ...