django-debug-toolbar:'Template'对象没有属性'引擎' [英] django-debug-toolbar: 'Template' object has no attribute 'engine'
问题描述
我刚刚尝试在新的计算机上运行一个现有的Django项目,而且我在django-debug-toolbar上遇到麻烦。这似乎与Jinja2有关。这是堆栈跟踪:
I've just tried running an existing Django project on a new computer, and I'm having trouble with django-debug-toolbar. It seems to be something to do with Jinja2. Here's the stack trace:
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整合到我的项目中,这样以前工作得很好,但是现在似乎期望这个模板
变量是一个正常的Django模板。在我的 TEMPLATES
设置中,我已经设置了Jinja2和DjangoTemplates,Jinja2使用了一个特定的扩展名('tmpl'),以确保只有这些模板被Jinja2和其他一切使用可以通过DjangoTemplates后端。
I'm using django-jinja2 to integrate Jinja2 into my project, and this worked okay before but it now seems to be expecting this template
variable to be a normal Django template. In my TEMPLATES
setting I have both Jinja2 and DjangoTemplates set up, with Jinja2 using a specific extension ('tmpl') to make sure only those templates are used by Jinja2 and everything else can go through the DjangoTemplates backend.
有没有人看到这个错误之前使用django调试工具栏与Jinja2?如果需要,我可以发布更多的设置。
Has anyone seen this error before when using django debug toolbar with Jinja2? I can post more settings if needed.
编辑:根据要求,这里是我的模板设置:
As requested, here's my TEMPLATES settings:
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",
]
}
}
]
更新 - 我通过制作修正了问题调试工具栏源代码中的一个小代码:从 debug_toolbar / panels / templates / panel.py
中更改第175行:
Update - I've "fixed" the issue by making a small code change in the debug toolbar source: changing line 175 in debug_toolbar/panels/templates/panel.py
from:
template_dirs = self.templates[0]['template'].engine.dirs
to:
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模板有后端
属性和Django模板有引擎
属性,两者似乎都用于同一件事。
I haven't looked into why this works (for some people this combination of debug toolbar 1.5 and django-jinja 2.2.0 works fine) but I noticed that Jinja2 templates have the backend
attribute and the Django templates have the engine
attribute, and both appear to be used for the same thing.
推荐答案
我也得到这个,你可以根据你的建议来修复它,而不是通过提供你自己的面板类来攻击核心:
I get this too, you can hack it fixed based on your suggestion without hacking core by supplying your own panel class:
debug.py
from debug_toolbar.panels.templates import TemplatesPanel as BaseTemplatesPanel
class TemplatesPanel(BaseTemplatesPanel):
def generate_stats(self, *args):
template = self.templates[0]['template']
if not hasattr(template, 'engine') and hasattr(template, 'backend'):
template.engine = template.backend
return super().generate_stats(*args)
settings.py / p>
settings.py
DEBUG_TOOLBAR_PANELS = [
'debug_toolbar.panels.versions.VersionsPanel',
'debug_toolbar.panels.timer.TimerPanel',
'debug_toolbar.panels.settings.SettingsPanel',
'debug_toolbar.panels.headers.HeadersPanel',
'debug_toolbar.panels.request.RequestPanel',
'debug_toolbar.panels.sql.SQLPanel',
'debug_toolbar.panels.staticfiles.StaticFilesPanel',
'myapp.debug.TemplatesPanel', # original broken by django-jinja, remove this whole block later
'debug_toolbar.panels.cache.CachePanel',
'debug_toolbar.panels.signals.SignalsPanel',
'debug_toolbar.panels.logging.LoggingPanel',
'debug_toolbar.panels.redirects.RedirectsPanel',
]
这篇关于django-debug-toolbar:'Template'对象没有属性'引擎'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!