Reputation: 1169
I have followed the instructions on using jinja2 with django1.8. --->
#settings.py
TEMPLATES = [
{
'BACKEND': 'django.template.backends.jinja2.Jinja2',
'DIRS': [
normpath(join(DJANGO_ROOT, 'templates/jinja2')),
],
'APP_DIRS': True,
'OPTIONS': {
'environment': 'kuyuweb_dj_1_8.jinja2.environment',
},
},
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [],
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
'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',
'django.core.context_processors.request',
],
},
},
]
I have a .py file including environment -->
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
and in my application folder i have templates/jinja2 folder. I have created a simple view as:
def home(request):
works = Work.objects.filter(publish=True).order_by('-created_at')[:8]
return render(request, 'jinja2/home.html', {'works':works })
But for example when i try to use jinja2 template tag as {{ loop.index }}
it does not work. {{ forloop.counter }}
is still working.
Is there something that i miss?
Upvotes: 1
Views: 682
Reputation: 308779
The jinja templates for your app should be in yourapp/jinja2
, not yourapp/templates/jinja2
.
If the template is at yourapp/jinja2/home.html
, then your render
line should be
return render(request, 'home.html', {'works':works })
Upvotes: 1