无法使用 Django 设置 Jinja2

Unable to set up Jinja2 with Django

我已经安装了 Django 1.9.7,Pythons 3.4.3 和 2.7.10 在 Ubuntu。

这些是我遵循的步骤:

  1. django-admin startproject testproject
  2. 创建了一个新项目
  3. cd testproject/testproject
  4. django-admin startapp testapp
  5. 的项目中制作了一个应用程序
  6. 使用 mkdir testapp/templates 在该应用程序中创建了一个模板目录,并在其中添加了一个非常基本的 index.html 模板
  7. 通过编辑默认设置文件的第 57 行编辑 settings.py 将模板后端更改为 django.template.backends.jinja2.Jinja2,并将 testproject.testapp 添加到 [=22] =]; TEMPLATES 部分因此是这样的:

    TEMPLATES = [
        {
            'BACKEND': 'django.template.backends.jinja2.Jinja2',
            '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',
                ],
            },
        },
    ]
    
  8. 编辑了 urls.py,添加了 from testproject.testapp import views 和 URL 模式 url(r'^$', views.index),

  9. 编辑testapp/views.py添加

    def index(request):
        return render(request, 'index.html')
    
  10. cd ..

  11. 运行 与 python3 manage.py runserverpython manage.py runserver 的服务器 -- 非常相似的效果
  12. 使用浏览器http://localhost:3000

我收到一个错误。在命令行上我得到这个:

Internal Server Error: /
Traceback (most recent call last):
  File "/usr/local/lib/python3.4/dist-packages/django/template/utils.py", line 86, in __getitem__
    return self._engines[alias]
KeyError: 'jinja2'

接着是另一个导致"during handling of the above exception"的异常,这与我在浏览器中看到的异常相匹配:

Environment:


Request Method: GET
Request URL: http://localhost:3000/

Django Version: 1.9.7
Python Version: 3.4.3
Installed Applications:
['django.contrib.staticfiles', 'testproject.testapp', 'webpack_loader']
Installed Middleware:
['django.middleware.security.SecurityMiddleware',
 'django.middleware.common.CommonMiddleware',
 'django.middleware.clickjacking.XFrameOptionsMiddleware']



Traceback:

File "/usr/local/lib/python3.4/dist-packages/django/template/utils.py" in __getitem__
  86.             return self._engines[alias]

During handling of the above exception ('jinja2'), another exception occurred:

File "/usr/local/lib/python3.4/dist-packages/django/core/handlers/base.py" in get_response
  174.                     response = self.process_exception_by_middleware(e, request)

File "/usr/local/lib/python3.4/dist-packages/django/core/handlers/base.py" in get_response
  172.                     response = response.render()

File "/usr/local/lib/python3.4/dist-packages/django/template/response.py" in render
  160.             self.content = self.rendered_content

File "/usr/local/lib/python3.4/dist-packages/django/template/response.py" in rendered_content
  135.         template = self._resolve_template(self.template_name)

File "/usr/local/lib/python3.4/dist-packages/django/template/response.py" in _resolve_template
  90.         new_template = self.resolve_template(template)

File "/usr/local/lib/python3.4/dist-packages/django/template/response.py" in resolve_template
  80.             return select_template(template, using=self.using)

File "/usr/local/lib/python3.4/dist-packages/django/template/loader.py" in select_template
  55.     engines = _engine_list(using)

File "/usr/local/lib/python3.4/dist-packages/django/template/loader.py" in _engine_list
  143.     return engines.all() if using is None else [engines[using]]

File "/usr/local/lib/python3.4/dist-packages/django/template/utils.py" in all
  110.         return [self[alias] for alias in self]

File "/usr/local/lib/python3.4/dist-packages/django/template/utils.py" in <listcomp>
  110.         return [self[alias] for alias in self]

File "/usr/local/lib/python3.4/dist-packages/django/template/utils.py" in __getitem__
  101.             engine = engine_cls(params)

File "/usr/local/lib/python3.4/dist-packages/django/template/backends/jinja2.py" in __init__
  35.         self.env = environment_cls(**options)

Exception Type: TypeError at /
Exception Value: __init__() got an unexpected keyword argument 'context_processors'

我得到的痕迹与 Python 2.

非常相似

我发现 which has a similar error message (KeyError: 'jinja2') but seems to be a separate problem, and this bug report又出现了同样的错误,其解决方法是安装jinja2,但是jinja2确实安装了。至少,我可以 运行 pythonpython3 然后 import jinja2pip 说 jinja2 是最新的。

我一定遗漏了一些重要的东西——有什么想法吗?

有关 context_processors 的错误是因为 Jinja2 后端不支持该参数。

您应该在 TEMPLATES 设置中添加一个额外的后端,而不是将现有后端从 django 替换为 jinja2。有关详细信息,请参阅 this answer。如果替换现有后端,则需要 Django 模板的应用程序将无法运行,包括管理员。

最后,jinja2 后端将在 testapp/jinja2 中查找模板,而不是 testapp/templates