1、安装Jinja2模块:
pip install jinja2
2、配置settings:
TEMPLATES = [ { ‘BACKEND‘: ‘django.template.backends.jinja2.Jinja2‘, ‘DIRS‘: [os.path.join(BASE_DIR, ‘templates‘)], ‘APP_DIRS‘: True, ‘OPTIONS‘: { ‘environment‘:‘MirMobAdmin.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‘, ], }, }, ]
3、创建MirMobAdmin/jinja2.py文件:
#!/usr/bin/python #coding:utf-8 from __future__ import absolute_import # Python 2 only 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
此文件的作用是把static和url这两个模板标签添加到Jinja2模板里面
4、模板里面由原来的{% static ‘xxx.js‘ %} 改为 {{ static(‘xxx.js‘) }}
目前测试正常!
时间: 2024-11-05 14:54:22