我刚刚尝试在新计算机上运行现有的Django项目,并且遇到了django-debug-toolbar问题。这似乎与Jinja2有关。这里的堆栈跟踪:django-debug-toolbar:'模板'对象没有属性'引擎'
Traceback:
File "/path/to/myrepo/env/local/lib/python2.7/site-packages/django/core/handlers/base.py" in get_response
223. response = middleware_method(request, response)
File "/path/to/myrepo/env/local/lib/python2.7/site-packages/debug_toolbar/middleware.py" in process_response
120. panel.generate_stats(request, response)
File "/path/to/myrepo/env/local/lib/python2.7/site-packages/debug_toolbar/panels/templates/panel.py" in generate_stats
175. context_processors = self.templates[0]['context_processors']
Exception Type: AttributeError at /first/page/
Exception Value: 'Template' object has no attribute 'engine'
我使用的Django使用Jinja2 Jinja2的要融入我的项目,而在此之前的工作不错,但现在似乎在期待这个template
变量是一个普通的Django模板。在我的TEMPLATES
设置中,我设置了Jinja2和DjangoTemplates,Jinja2使用特定的扩展名('tmpl')确保Jinja2只使用那些模板,其他所有模板都可以通过DjangoTemplates后端。
在Jinja2中使用django调试工具栏之前有没有人看到过这个错误?如果需要,我可以发布更多设置。
编辑:按照要求,这里是我的模板设置:
TEMPLATES = [
{
#'BACKEND': 'django.template.backends.jinja2.Jinja2',
'BACKEND': 'django_jinja.backend.Jinja2',
#'NAME': 'jinja2',
'DIRS': [
os.path.join(DEPLOY_PATH, 'templates')
],
'APP_DIRS': True,
'OPTIONS': {
'debug': DEBUG,
'match_extension': '.tmpl',
#'environment': 'jinja2.Environment',
'extensions': [
'jinja2.ext.with_',
'jinja2.ext.i18n',
'django_jinja.builtins.extensions.UrlsExtension',
'django_jinja.builtins.extensions.CsrfExtension',
'pipeline.templatetags.ext.PipelineExtension',
],
'context_processors': [
"django.contrib.auth.context_processors.auth",
"django.core.context_processors.debug",
"django.core.context_processors.i18n",
"django.core.context_processors.media",
"django.core.context_processors.static",
"django.contrib.messages.context_processors.messages",
"django.core.context_processors.request",
]
},
},
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [
os.path.join(DEPLOY_PATH, 'templates')
],
'APP_DIRS': True,
'OPTIONS': {
'debug': DEBUG,
'context_processors': [
"django.contrib.auth.context_processors.auth",
"django.core.context_processors.debug",
"django.core.context_processors.i18n",
"django.core.context_processors.media",
"django.core.context_processors.static",
"django.contrib.messages.context_processors.messages",
"django.core.context_processors.request",
]
}
}
]
更新 - 我已经通过在调试工具栏源的小代码变“固定”的问题:改变线路175 debug_toolbar/panels/templates/panel.py
来自:
template_dirs = self.templates[0]['template'].engine.dirs
到:
if hasattr(self.templates[0]['template'], 'engine'):
template_dirs = self.templates[0]['template'].engine.dirs
elif hasattr(self.templates[0]['template'], 'backend'):
template_dirs = self.templates[0]['template'].backend.dirs
else:
raise RuntimeError("Couldn't find engine or backend for a template: {}",format(self.templates[0]['template']))
我还没有看过为什么这个工程(对于一些人来说,调试工具栏1.5和django-jinja 2.2.0的这个组合工作正常),但我注意到Jinja2模板具有backend
属性,并且Django模板具有engine
属性,并且两者似乎都用于同样的事情。