LauraBen
LauraBen

Reputation: 119

Jinja2 throwing templatedoesnotexist error in Django project

I'm trying to use jinja2 in my Django project (ver 1.10). After setting it up, once I try to run the project, I get TemplateDoesNotExist at /base/index.html and

Template-loader postmortem

Django tried loading these templates, in this order:

Using engine jinja2:
This engine did not provide a list of tried templates.

In templates folder, I have a base directory where index.html is placed. Moreover, if I use Django's template engine, this same folder structure works perfectly. How can I fix this issue?


My config is as follows:

In settings.py (note that I've deliberately excluded the Django template fallback):

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

I also have a file called jinja2.py kept in the project folder:

from __future__ import absolute_import 
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

Upvotes: 5

Views: 3596

Answers (1)

LauraBen
LauraBen

Reputation: 119

This was my BASE_DIR and MAIN_DIR in settings.py

BASE_DIR = os.path.dirname(os.path.abspath(__file__))
MAIN_DIR = os.path.dirname(os.path.dirname(__file__))

and my jinja2 templates were at the path

/Users/username/Desktop/uberfordoc/templates/jinja2

changed

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

to

'DIRS': [os.path.join(MAIN_DIR, 'templates/jinja2')],

and it worked. Got the hint thanks to @Timbadu by printing

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

Upvotes: 3

Related Questions