【发布时间】:2016-10-09 09:45:54
【问题描述】:
我已经安装了 Django 1.9.7,并且我在 Ubuntu 上安装了 Python 3.4.3 和 2.7.10。
这些是我遵循的步骤:
- 用
django-admin startproject testproject创建了一个新项目 cd testproject/testproject- 在项目中使用
django-admin startapp testapp制作了一个应用程序 - 使用
mkdir testapp/templates在该应用程序中创建了一个模板目录,并在其中添加了一个非常基本的index.html模板 -
编辑
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', ], }, }, ] 编辑
urls.py,添加from testproject.testapp import views和 URL 模式url(r'^$', views.index),-
编辑
testapp/views.py添加def index(request): return render(request, 'index.html') cd ..- 使用
python3 manage.py runserver或python manage.py runserver运行服务器——效果非常相似 - 用浏览器访问
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。至少,我可以运行python 或python3,然后运行import jinja2。 pip 说 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 不是其中之一。