简体   繁体   English

Jinja2模板在Django 1.9中不起作用

[英]Jinja2 templating not working in in Django 1.9

Per the instructions on the Django 1.9 tutorial I've added another file in the project root with the Environment settings - 按照Django 1.9教程中的说明,我在项目根目录中使用环境设置添加了另一个文件-

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`

(Granted to load the proper jinja2 I had to rename the file something differently, in this case jinja2env.py in project root) (允许加载正确的jinja2我不得不以其他方式重命名该文件,在这种情况下,项目根目录下为jinja2env.py

And I updated settings.py with the new templating backend: 然后,我使用新的模板后端更新了settings.py

TEMPLATES = [
{
    'BACKEND': 'django.template.backends.django.DjangoTemplates',
    'DIRS': [os.path.join(PROJECT_ROOT, 'templates').replace('\\','/')],
    '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',
        ],
    },
},
{
    'BACKEND': "django.template.backends.jinja2.Jinja2",
    'DIRS': [os.path.join(PROJECT_PATH, 'campaigns/templates').replace('\\','/')],
    "APP_DIRS": True,
    "OPTIONS": {
        'environment': 'jinja2env.Environment',
    }
},

In the view I'm working on I use the using parameter to specify the jinja2 templating engine: 在我正在处理的视图中,我使用using参数指定jinja2模板引擎:

return render(request, 'jinja2/index.html', context={'projects': projects, 'counter': 0}, status=200, using='jinja2')

Yet when the template goes to render I have following error: 'static' is undefined . 但是,当模板渲染时,出现以下错误: 'static' is undefined Clearly my setup is wrong or I am not doing something correct. 显然我的设置有误,或者我做的不正确。 The template starts as such: 模板开始如下:

<link rel="stylesheet" type="text/css" href="{{ static('stylesheets/main.css') }}">

What am I doing wrong? 我究竟做错了什么? I don't use {% load static %} since it isn't a Django template ... so I'm at a loss. 我不使用{% load static %}因为它不是Django模板...所以我很茫然。

You're loading the wrong environment. 您正在加载错误的环境。 In your code jinja2env.Environment actually refers to default Environment from jinja2.Environment . 在你的代码jinja2env.Environment实际上指的是从默认环境jinja2.Environment

"OPTIONS": {
    'environment': 'jinja2env.Environment',
}

should be changed to 应该更改为

"OPTIONS": {
    'environment': 'jinja2env.environment',
}

Notice the lowercase environment , which is the environment you defined inside jinja2env.py . 注意小写environment ,这是您在jinja2env.py定义的环境。

根据您的设置和对该问题的可接受答案,您似乎应该尝试添加static上下文处理器。

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

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