LPB
LPB

Reputation: 515

Django can't find template directory

I know there are many questions similar to this one, but none has solved my problem yet.

Python version: 3.4 Django version: 1.8

I get TemplateDoesNotExist at /lfstd/ when loading http://127.0.0.1:8000/lfstd/.

system.py:

BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
TEMPLATE_PATH = os.path.join(BASE_DIR, 'templates')
TEMPLATE_DIRS = (
    TEMPLATE_PATH,
)

lfstd/views.py:

def index(request):
[...]
return render(request, 'lfstd/index.html', context_dict)

project urls.py:

from django.conf.urls import include, url
from django.contrib import admin

url(r'^admin/', include(admin.site.urls)),
url(r'^lfstd/', include('lfstd.urls')),

lfstd urls.py:

from django.conf.urls import patterns, url
from lfstd import views

urlpatterns = patterns('',
    url(r'^$', views.index, name='index'))

Running print on base_dir and template_path, I get:

Base dir: C:\Users\Phil\PycharmProjects\TownBuddies
Template path: C:\Users\Phil\PycharmProjects\TownBuddies\templates

Which is exactly where my project and template folder is located. However, django doesn't look in that folder for templates. It looks in the following folders:

C:\Python34\lib\site-packages\django\contrib\admin\templates\lfstd\index.html (File does not exist)
C:\Python34\lib\site-packages\django\contrib\auth\templates\lfstd\index.html (File does not exist)

In fact, if I move my template there, it does find it and it works.

Any help is very appreciated, I'm starting out with Django and completely stuck...

EDIT:

I changed TEMPLATE_DIRS to TEMPLATES but Django still isn't looking in the templates folder: from the django debug report

Upvotes: 6

Views: 10533

Answers (2)

dmmfll
dmmfll

Reputation: 2836

I was doing some unit testing with pytest-django when suddenly I was getting repeated and completely unexpected ** TemplateDoesNotExist** errors.

I found this stackoverflow question while trying to figure out what was going on.

I finally solved the issue by deleting all the __pycache__ directories. I have since learned that when things suddenly go inexplicably wonky to clear out the __pycache__ before breaking things by trying to fix them!

I used this script to clear out all the __pycache__ directories recursively where a module with the following resides:

#!/usr/bin/env python
import os
import shutil

#!/usr/bin/env python

import os
import shutil

BASE_DIR = os.path.abspath(__file__)
print(BASE_DIR)

for root, dirs, files in os.walk(BASE_DIR):
    for directory in dirs:
        if directory == '__pycache__':
            shutil.rmtree(os.path.join(root, directory))

Upvotes: 1

catavaran
catavaran

Reputation: 45575

TEMPLATE_DIRS setting is deprecated in django 1.8. You should use the TEMPLATES instead:

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

Upvotes: 17

Related Questions