Alesito
Alesito

Reputation: 605

Django not looking for templates in the correct directory

I have one existing Django project, which works fine. I made a new one then, based on the original one (not copying, only using the structure) and I have a problem with the new one.

In the app in the new project I have a folder templates and in that one another folder with app name. But this doesn't load the template if I reference it as 'app/index.html' in the view.

My TEMPLATE_DIRS is set to os.path.join(BASE_DIR, 'templates') just as it is with the first project, which works. The directory structure is the same. But when I get the debug message I can see that it's trying to load templates from ROOT/templates/app/index.html instead of ROOT/app/templates/app/index.html.

What I at the end find out to be working was, to est TEMPLATE_DIRS to BASE_DIR only and then reference the complete path to the template as 'app/templates/app/index.html'. But this is tedious and obviously not correct.

In neither of the apps am I using TEMPLATE_LOADERS, it's all default. What else can I check?

I'm using Django 1.6.1 on Python 2.7.

Upvotes: 2

Views: 3213

Answers (2)

Akhil Suresh
Akhil Suresh

Reputation: 151

Most often this kind of errors happens when we name the templates folder wrong inside our app. Check whether it is named correctly as "templates". django.template.loaders.app_directories.Loader looks for a folder named templates inside our app folder

i.e,

app/
     templates/
               app/
                   index.html

Also make sure that you have correctly defined your app name inside INSTALLED_APPS as 'app_name.apps.App_nameConfig':

INSTALLED_APPS = [    
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'app_name.apps.App_nameConfig']

Upvotes: 2

user3681414
user3681414

Reputation: 109

Looking at your TEMPLATE_DIRS, I would expect it to look under /full/path/to/base_dir/templates, not /full/path/to/base_dir/app/templates

If the latter is what you want, use

os.path.join(BASE_DIR', 'app', 'templates')

Upvotes: 2

Related Questions