Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django 1.8 TEMPLATE_DIRS being ignored

Tags:

python

django

This is driving me crazy. I've done something weird and it appears that my TEMPLATE_DIRS entries are being ignored. I have only one settings.py file, located in the project directory, and it contains:

TEMPLATE_DIRS = (
    os.path.join(BASE_DIR, 'templates'),
    os.path.join(BASE_DIR, 'web_app/views/'),
)

I'm putting project-level templates in the /templates folder, and then have folders for different view categories in my app folder (e.g. authentication views, account views, etc.).

For example, my main index page view is in web_app/views/main/views_main.py and looks like

from web_app.views.view_classes import AuthenticatedView, AppView


class Index(AppView):
    template_name = "main/templates/index.html"

where an AppView is just an extension of TemplateView. Here's my problem: when I try to visit the page, I get a TemplateDoesNotExist exception and the part that's really confusing me is the Template-Loader Postmortem:

Template-loader postmortem

Django tried loading these templates, in this order:
Using loader django.template.loaders.filesystem.Loader:
Using loader django.template.loaders.app_directories.Loader:
C:\Python34\lib\site-packages\django\contrib\admin\templates\main\templates\index.html (File does not exist)
C:\Python34\lib\site-packages\django\contrib\auth\templates\main\templates\index.html (File does not exist)

Why in the world are the 'templates' and 'web_app/views' directories not being searched? I've checked Settings via the debugger and a breakpoint in views_main.py and it looks like they're in there. Has anyone had a similar problem? Thanks.

like image 317
Gadzooks34 Avatar asked May 15 '15 14:05

Gadzooks34


1 Answers

What version of Django are you using? TEMPLATE_DIRSis deprecated since 1.8

Deprecated since version 1.8: Set the DIRS option of a DjangoTemplates backend instead.

https://docs.djangoproject.com/en/1.8/ref/settings/#template-dirs

So try this instead:

TEMPLATES = [
  {
    'BACKEND': 'django.template.backends.django.DjangoTemplates',
    'DIRS': [
        # insert your TEMPLATE_DIRS here
    ],
    'APP_DIRS': True,
    'OPTIONS': {
        'context_processors': [
            # Insert your TEMPLATE_CONTEXT_PROCESSORS here or use this
            # list if you haven't customized them:
            'django.contrib.auth.context_processors.auth',
            'django.template.context_processors.debug',
            'django.template.context_processors.i18n',
            'django.template.context_processors.media',
            'django.template.context_processors.static',
            'django.template.context_processors.tz',
            'django.contrib.messages.context_processors.messages',
        ],
    },

  },
]

Here's a link to an upgrade guide: https://docs.djangoproject.com/en/1.8/ref/templates/upgrading/

like image 195
JOSEFtw Avatar answered Nov 07 '22 05:11

JOSEFtw