简体   繁体   English

如何设置django 1.8使用jinja2?

[英]How to setup django 1.8 to use jinja2?

So, now that django officially supports Jinja 2 as a templating engine, I hoped enabling it would be as simple as switching a line in config. 所以,既然django正式支持Jinja 2作为模板引擎,我希望启用它就像在配置中切换一行一样简单。 But when I do that, jinja fails to find my templates. 但是当我这样做时,jinja找不到我的模板。

My understanding is I could manually configure a list of directories for it to look for templates in, but I would like it to behave exactly like DTL behaves by default. 我的理解是我可以手动配置目录列表以便查找模板,但我希望它的行为与默认情况下的DTL行为完全相同。 (ie. look in the /templates directory). (即查看/ templates目录)。 Basically, my app is structured the way it is suggested in the official tutorial, and I would like to use jinja without changing anything else. 基本上,我的应用程序的结构与官方教程中的建议方式相同,我想在不改变任何其他内容的情况下使用jinja。 Is it possible? 可能吗?

Here's how my setings.py file looks now: 这是我的setings.py文件现在的样子:

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.jinja2.Jinja2',
        'APP_DIRS': True,
    },
]

The error I get is TemplateDoesNotExist at / 我得到的错误是TemplateDoesNotExist at /

and here is my directory structure: 这是我的目录结构:

mysite
    mysite
    myapp
        templates
            myapp  
                index.html
    manage.py

please note that I am hoping not to use any external modules. 请注意,我希望不使用任何外部模块。

edit: as requested, here's the code calling the template: 编辑:根据要求,这是调用模板的代码:

def index(request):
    return render(request, 'myapp/index.html')

The Jinja template folder for app dirs defaults to jinja2 not the standard templates folder. app dirs的Jinja模板文件夹默认为jinja2而不是标准templates文件夹。

So try the following directory structure and Django will locate your Jinja templates: 因此,尝试以下目录结构,Django将找到您的Jinja模板:

mysite
    mysite
    myapp
        jinja2
            myapp  
                index.html
    manage.py

Another thing to consider is that render_to_response can not take a context_instance for jinja2 templates 另一件需要考虑的事情是render_to_response不能为jinja2模板采用context_instance

https://github.com/django-haystack/django-haystack/issues/1163 https://github.com/django-haystack/django-haystack/issues/1163

I believe, but I might be wrong, but I think jinja2 can't share the same directory as the django templates. 我相信,但我可能错了,但我认为jinja2不能与django模板共享同一目录。 try 尝试

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

The Jinja2 template backend searches the jinja2 folder in the app directories, instead of templates . Jinja2模板后端搜索应用程序目录中的jinja2文件夹,而不是templates This has the advantange of preventing DTL and Jinja2 templates from being confused, especially if you enable multiple templating engines in your project. 这有利于防止DTL和Jinja2模板混淆,特别是如果在项目中启用多个模板引擎。

I would recommend sticking with the default behaviour, and renaming your templates directory to jinja2 . 我建议坚持默认行为,并将您的templates目录重命名为jinja2 However, if you must change it, you could create a custom backend, and set app_dirname . 但是,如果必须更改它,则可以创建自定义后端,并设置app_dirname

from django.template.backends.jinja2 import Jinja2

class MyJinja2(jinja2):
    app_dirname = 'templates'

Then in your TEMPLATES setting, use path.to.MyJinja2 instead of django.template.backends.jinja2.Jinja2 . 然后在TEMPLATES设置中,使用path.to.MyJinja2而不是django.template.backends.jinja2.Jinja2

The Jinja template folder for app dirs defaults to jinja2 not the standard templates folder. app dirs的Jinja模板文件夹默认为jinja2而不是标准模板文件夹。

So try the following directory structure and Django will locate your Jinja templates: 因此,尝试以下目录结构,Django将找到您的Jinja模板:

mysite mysite myapp jinja2 myapp mysite mysite myapp jinja2 myapp
index.html manage.py index.html manage.py

And instead of: return render(request, 'myapp/index.html') you should write: return render(request, 'index.html') 而不是:return render(request,'myapp / index.html')你应该写:return render(request,'index.html')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM