我在这里关注这个帖子...... How to use jinja2 as a templating engine in Django 1.8
我似乎无法找到任何关于切换到jinja2模板以及如何为它设置django的可靠文档。所有的文档似乎都有一些差距,或者我错过了一些非常明显的东西。无论如何,我设置了所有内容,就像上面的线程一样,除非我试图去某个模板加载的地方,我得到...
TemplateDoesNotExist
Django tried loading these templates, in this order:
...
但它没有列出我指定的任何jinja / template目录。为什么会这样?
编辑添加痕迹和设置:
TEMPLATES = (
{
'BACKEND': 'askbot.skins.template_backends.AskbotSkinTemplates',
},
{
'BACKEND': 'django.template.backends.jinja2.Jinja2',
'DIRS': [os.path.join(PROJECT_ROOT, 'templates/jinja2')],
'APP_DIRS': True,
'OPTIONS': {'environment': 'myproject.jinja-import.Environment',},
},
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [
os.path.join(PROJECT_ROOT, 'templates',),
],
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
'django.core.context_processors.request',
'django.contrib.auth.context_processors.auth',
]
}
},
)
Traceback:
File "/home/jeff/Django/langalang-askbot/venv/local/lib/python2.7/site-packages/Django-1.8.8-py2.7.egg/django/core/handlers/base.py" in get_response
132. response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/home/jeff/Django/langalang-askbot/langalang/main/views.py" in page
71. context_instance=RequestContext(request, context))
File "/home/jeff/Django/langalang-askbot/venv/local/lib/python2.7/site-packages/Django-1.8.8-py2.7.egg/django/shortcuts.py" in render
89. using=using)
File "/home/jeff/Django/langalang-askbot/venv/local/lib/python2.7/site-packages/Django-1.8.8-py2.7.egg/django/template/loader.py" in render_to_string
137. raise TemplateDoesNotExist(template_name)
Exception Type: TemplateDoesNotExist at /ko/
Exception Value: main/page.html
Template Loader Error:
Django tried loading these templates, in this order:
Using loader django.template.loaders.filesystem.Loader:
/home/jeff/Django/langalang-askbot/langalang/templates/main/page.html (File does not exist)
Using loader django.template.loaders.app_directories.Loader:
/home/jeff/Django/langalang-askbot/venv/local/lib/python2.7/site-packages/Django-1.8.8-py2.7.egg/django/contrib/auth/templates/main/page.html (File does not exist)
/home/jeff/Django/langalang-askbot/venv/local/lib/python2.7/site-packages/Django-1.8.8-py2.7.egg/django/contrib/admin/templates/main/page.html (File does not exist)
/home/jeff/Django/langalang-askbot/venv/local/lib/python2.7/site-packages/Django-1.8.8-py2.7.egg/django/contrib/sitemaps/templates/main/page.html (File does not exist)
/home/jeff/Django/langalang-askbot/venv/local/lib/python2.7/site-packages/django_compressor-1.5-py2.7.egg/compressor/templates/main/page.html (File does not exist)
/home/jeff/Django/langalang-askbot/langalang/askbot-dev/askbot/templates/main/page.html (File does not exist)
/home/jeff/Django/langalang-askbot/venv/local/lib/python2.7/site-packages/django_robots-1.1-py2.7.egg/robots/templates/main/page.html (File does not exist)
/home/jeff/Django/langalang-askbot/venv/local/lib/python2.7/site-packages/django_celery-3.1.17-py2.7.egg/djcelery/templates/main/page.html (File does not exist)
/home/jeff/Django/langalang-askbot/venv/local/lib/python2.7/site-packages/django_tinymce-1.5.3-py2.7.egg/tinymce/templates/main/page.html (File does not exist)
/home/jeff/Django/langalang-askbot/venv/local/lib/python2.7/site-packages/django_recaptcha-1.0.5-py2.7.egg/captcha/templates/main/page.html (File does not exist)
/home/jeff/Django/langalang-askbot/venv/local/lib/python2.7/site-packages/django_avatar-2.2.1-py2.7.egg/avatar/templates/main/page.html (File does not exist)
正在加载模板的视图:
def page(request, page_title='home'):
context = {
'page_title': page_title
}
if page_title == 'home':
#This query pulls the language instructions objects and then orders it alphabetically by the 'full_title' field
language_pairs = LangPairInstruction.objects.filter(active=True).order_by('full_title')
context['language_pairs'] = language_pairs
elif page_title == 'contact':
form_class = ContactForm(initial={'subject': _('Subject'), 'name': _('Name'), 'email': _('Email'), 'content': _('Content')})
context['ContactForm'] = form_class
elif page_title == 'about':
pass
else:
raise Http404
return render(
request,
'main/page.html',
context_instance=RequestContext(request, context))