阿小信大人的头像
Life is short (You need Python) Bruce Eckel

Django使用Jinja22014-09-02 06:49

先pip安装jinja2。

settings.py中修改TEMPLATE_LOADERS:

TEMPLATE_LOADERS = (
    'jinja2loader.Loader', 
  #  'django.template.loaders.filesystem.Loader',
  #  'django.template.loaders.app_directories.Loader',
  #  'django.template.loaders.eggs.Loader',
)

settings.py同级目录新建文件jinja2loader.py

""" 
See http://docs.djangoproject.com/en/dev/ref/templates/api/#using-an-alternative-template-language

Use: 
 * {{ url_for('view_name') }} instead of {% url view_name %}, 
 * <input type="hidden" name="csrfmiddlewaretoken" value="{{ csrf_token }}"> 
   instead of {% csrf_token %}.

"""

from django.template.loader import BaseLoader  
from django.template import TemplateDoesNotExist  
from django.core import urlresolvers  
from django.conf import settings  
import jinja2


class Template(jinja2.Template):  
    def render(self, context):  
        # flatten the Django Context into a single dictionary.  
        context_dict = {}  
        for d in context.dicts:  
            context_dict.update(d)  
        return super(Template, self).render(context_dict)

class Loader(BaseLoader):  
    is_usable = True  
    env = jinja2.Environment(loader=jinja2.FileSystemLoader(settings.TEMPLATE_DIRS))  
    env.template_class = Template

    # These are available to all templates.  
    env.globals['url_for'] = urlresolvers.reverse  
    env.globals['MEDIA_URL'] = settings.MEDIA_URL  
    #env.globals['STATIC_URL'] = settings.STATIC_URL


    def load_template(self, template_name, template_dirs=None):  
        try:  
            template = self.env.get_template(template_name)  
        except jinja2.TemplateNotFound:  
            raise TemplateDoesNotExist(template_name)  
        return template, template.filename

这样模板就使用的是jinja2了

如果您觉得从我的分享中得到了帮助,并且希望我的博客持续发展下去,请点击支付宝捐赠,谢谢!

若非特别声明,文章均为阿小信的个人笔记,转载请注明出处。文章如有侵权内容,请联系我,我会及时删除。

#Python#   #django
分享到:
阅读[3046] 评论[1]

你可能也感兴趣的文章推荐

本文最近访客

发表评论

#1 网友106.*.*.3[重庆]2708 :
博主,我这儿怎么不行呢?拿到我漏掉什么了吗? ImportError at /mmsite/ No module named jinja2loader Request Method: GET Request URL: http://localhost:8001/mmsite/ Django Version: 1.7.1 Exception Type: ImportError Exception Value: No module named jinja2loader Exception Location: D:\Python27\lib\importlib\__init__.py in import_module, line 37 Python Executable: D:\Python27\python.exe Python Version: 2.7.8 Python Path: ['E:\\medical management\\mmsite', 'D:\\Python27\\lib\\site-packages\\setuptools-7.0-py2.7.egg', 'D:\\Python27\\lib\\site-packages\\jinja2-2.7.3-py2.7.egg', 'D:\\Python27\\lib\\site-packages\\markupsafe-0.23-py2.7.egg', 'D:\\Python27\\lib\\site-packages\\mysql_connector_python-2.0.1-py2.7.egg', 'D:\\Python27\\lib\\site-packages\\watchdog-0.8.1-py2.7.egg', 'D:\\Python27\\lib\\site-packages\\pathtools-0.1.2-py2.7.egg', 'D:\\Python27\\lib\\site-packages\\argh-0.26.0-py2.7.egg', 'D:\\Python27\\lib\\site-packages\\pyyaml-3.11-py2.7-win-amd64.egg', 'C:\\Windows\\system32\\python27.zip', 'D:\\Python27\\DLLs', 'D:\\Python27\\lib', 'D:\\Python27\\lib\\plat-win', 'D:\\Python27\\lib\\lib-tk', 'D:\\Python27', 'D:\\Python27\\lib\\site-packages'] Server time: Fri, 21 Nov 2014 17:31:54 +0800
2014-11-21 17:34 回复