6

I'm using Django 1.8, which allows you to switchout the Django Template Engine for Jinja2.

I have the following in my setting.py

TEMPLATES = [{
    'BACKEND': 'django.template.backends.jinja2.Jinja2',
    'DIRS': [
        os.path.join(BASE_DIR, 'templates/jinja2'),
    ],
    'APP_DIRS': True,
    'OPTIONS': {
        'environment': 'appname.jinja2.environment',
    }, 
},]

I'm basically using the instructions from http://jonathanchu.is/posts/upgrading-jinja2-templates-django-18-with-admin/, except that I'm trying to get the engine to find templates within the templates folder of each additional app. So, :

├── myproject
│   ├── __init__.py
│   ├── jinja2.py
│   ├── settings.py
│   ├── urls.py
│   └── wsgi.py
├── app2
│   ├── __init__.py
│   └──  other app stuff ...
├── templates
│   └── jinja2
│       └── app2
│           └── template.html
├── manage.py
├── templates
│   └── jinja2
│       ├── something.html

Both apps are installed:

INSTALLED_APPS = (
    ...
    'myproject',
    'app2',
)

myproject/jinja2.py contains:

from __future__ import absolute_import  # Python 2 only
from django.contrib.staticfiles.storage import staticfiles_storage
from django.core.urlresolvers import reverse
from jinja2 import Environment

def environment(**options):
    env = Environment(**options)
    env.globals.update({
       'static': staticfiles_storage.url,
       'url': reverse,
    })
    return env

but still, I get Exception Type: TemplateDoesNotExist when I use

from django.template.loader import get_template
template = get_template('app2/template.html') 

I've tried adding adding

TEMPLATE_LOADERS = (
    'django.template.loaders.filesystem.Loader',
    'django.template.loaders.app_directories.Loader',
)

tried adding {'loader': Loader} to 'OPTIONS' where Loader is from django.template.loaders.app_directories import Loader as well as using some Jinja2 loaders. None worked.

If I add the DjangoTemplates engine as a secondary, it finds the template. Jinja2 doesn't. Before, the Debug Traceback screen was showing that only django.template.loaders.filesystem.Loader was being used (right below where it says Traceback Switch to copy-and-paste view), but now it's not telling me anything.

I'm using Windows and the development server (manage.py runserver). I don't have any TEMPLATE_DIRS in settings.py (although hardcoding the particular apps template directory into TEMPLATE_DIRS also didn't fix the problem; In fact, I got the warning The standalone TEMPLATE_* settings were deprecated in Django 1.8..., When I remove this I get no issues (0 silenced))

I am NOT getting one of the following in my traceback:

Template Loader Error:
Django tried loading these templates, in this order:
Using loader django.template.loaders.filesystem.Loader:
Using loader django.template.loaders.app_directories.Loader:

Looked at:

Django can't find template dir?

Django can't find template

Django Can't Find App Templates

I'm out of ideas. Help.

Community
  • 1
  • 1
artdv
  • 774
  • 1
  • 8
  • 23
  • `environment: appname.jinja.Environment`, this section, try `environment: appname.jinja2.environment`, you have misspelled e to capitol E and add jinja2 instead of jinja. – copser Aug 24 '15 at 02:47
  • My mistake, however those only exist in the question, not in my code. – artdv Aug 24 '15 at 22:32
  • but your template path is not in `app2/template.html` it is `jinja2/app2/template.html` – Goran Jan 15 '21 at 16:13

0 Answers0