如何在Django 1.8中使用jinja2作为模板引擎 [英] How to use jinja2 as a templating engine in Django 1.8

查看:127
本文介绍了如何在Django 1.8中使用jinja2作为模板引擎的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在寻找如何在django 1.8中使用jinja2,但是没有使用django和jinja2的完整来源。我想知道你们是否知道在django中使用jinja2的过程。我已经查看了官方文档,我看过以下问题:如何设置django 1.8来使用jinja2?



但是没有一个可以清楚地解释如何用put-tother其他方式使用jinja2。我刚刚开始使用django,不知道文档中的所有语句。我真的很感激帮助。

解决方案

你必须安装 jinja2

  $ pip install Jinja2 

然后在 settings.py 中修改 TEMPLATES 列表,以包含 jinja2 BACKEND

  TEMPLATES = [

{
'BACKEND':'django.template.backends.jinja2.Jinja2',
'DIRS':[os.path.join(BASE_DIR,'templates / jinja2') ],
'APP_DIRS':True,
'OPTIONS':{'environment':'myproject.jinja2.Environment',},
},
{
'BACKEND':'django.template.backends.django.DjangoTemplates',
'DIRS':[],
'APP_DIRS':True,
'OPTIONS':{
'context_processors':[
'django.template.context_processors.debug',
'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
],
},
},
]

其中 templates / jinja2 是您的jinja2模板文件的目录。



在您的views.py文件中:

从__future__导入absolute_import#Python 2只有
从jinja2 import环境
from django.contrib.staticfiles.storage import staticfiles_storage
从django.core.urlresolvers import reverse

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

这使得您的Jinja2模板中可以使用 static url



PS 有关详细信息,请参阅这篇文章


I have been looking on how to use jinja2 in django 1.8, but there is no complete source for using django with jinja2. I was wondering if you guys knew the process for using jinja2 in django. I have looked through the the official documentation and I have looked at the following question: How to setup django 1.8 to use jinja2?

but none of them clearly explain how to use jinja2 in an put-togther manner. I just started using django and don't know all the lingo in the docs. I would really appreciate the help.

解决方案

Frist you have to install jinja2:

$ pip install Jinja2

Then modify your TEMPLATES list in the settings.py to contain the jinja2 BACKEND :

TEMPLATES = [

    {
        'BACKEND': 'django.template.backends.jinja2.Jinja2',
        'DIRS': [os.path.join(BASE_DIR, 'templates/jinja2')],
        'APP_DIRS': True,
        'OPTIONS': {'environment': 'myproject.jinja2.Environment',}, 
    },
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [],
        'APP_DIRS': True,
        'OPTIONS': {
        'context_processors': [
            'django.template.context_processors.debug',
            'django.template.context_processors.request',
            'django.contrib.auth.context_processors.auth',
            'django.contrib.messages.context_processors.messages',
           ],
        },
    },
]

where templates/jinja2 is the directory with your jinja2 template files.

And in your views.py file:

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

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

This makes static and url available in your Jinja2 templates.

P.S. For more details see this article.

这篇关于如何在Django 1.8中使用jinja2作为模板引擎的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆