【问题标题】:Unable to set up Jinja2 with Django无法使用 Django 设置 Jinja2
【发布时间】:2016-10-09 09:45:54
【问题描述】:

我已经安装了 Django 1.9.7,并且我在 Ubuntu 上安装了 Python 3.4.3 和 2.7.10。

这些是我遵循的步骤:

  1. django-admin startproject testproject创建了一个新项目
  2. cd testproject/testproject
  3. 在项目中使用django-admin startapp testapp 制作了一个应用程序
  4. 使用mkdir testapp/templates 在该应用程序中创建了一个模板目录,并在其中添加了一个非常基本的index.html 模板
  5. 编辑settings.py,将模板后端更改为django.template.backends.jinja2.Jinja2,方法是编辑默认设置文件的第57行,并将testproject.testapp添加到INSTALLED_APPS;因此,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',
                ],
            },
        },
    ]
    
  6. 编辑 urls.py,添加 from testproject.testapp import views 和 URL 模式 url(r'^$', views.index),

  7. 编辑testapp/views.py添加

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

  9. 使用python3 manage.py runserverpython manage.py runserver 运行服务器——效果非常相似
  10. 用浏览器访问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'

随后是另一个异常导致“在处理上述异常期间”,这与我在浏览器中看到的异常匹配:

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 中得到了非常相似的痕迹。

我发现this question 有类似的错误消息(KeyError: 'jinja2')但似乎是一个单独的问题,this bug report 再次出现相同的错误,其解决方案是安装 jinja2,但肯定安装了 jinja2。至少,我可以运行pythonpython3,然后运行import jinja2pip 说 jinja2 是最新的。

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

【问题讨论】:

  • 听起来您已将默认模板后端从 django 更改为 jinja - 最好将 Jinja 添加为附加后端(请参阅this answer。如果您替换 Django 模板后端,则应用使用 Django 模板将不再起作用,包括管理员。
  • 另外,Django 将在 testapp/jinja2 中查找 Jinja 模板,而不是 testapp/templates
  • mv testproject/testapp/templates testproject/testapp/jinja2 没有改变任何东西(我认为它甚至没有达到试图找到指定模板的程度),但知道这一点很有用;谢谢。然后我将模板后端设置回滚到库存,然后按照您的建议为 Jinja2 添加了一个新条目,但从链接的答案中完全省略了选项键(因为我还不知道 Environment 做什么)并将 DIRS 留空(因为APP_DIRS 会做我想做的事),它现在可以工作了。如果您将其写成答案,我会接受。谢谢!
  • 我在这个问题上特别明确,除了我更改的内容之外,它都是默认设置。 context_processors 默认情况下在那里——我没有碰过它,因为我不知道它是什么。关于 Jinja2 的文档不是很清楚,我从中了解到我所要做的就是更改 BACKEND
  • 卜,Jinja2 的文档是very comprehensive,准确显示了允许的选项; context_processors 不是其中之一。

标签: django jinja2


【解决方案1】:

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

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

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

【讨论】:

    猜你喜欢
    • 2020-06-18
    • 2015-07-29
    • 2013-06-02
    • 1970-01-01
    • 2021-09-21
    • 1970-01-01
    • 2014-06-06
    • 1970-01-01
    • 2017-06-11
    相关资源
    最近更新 更多