2016-07-25 72 views
1

我刚刚尝试在新计算机上运行现有的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属性,并且两者似乎都用于同样的事情。

回答

2

我得到这个也一样,你可以破解它修好根据您的建议,而不通过提供自己的面板类黑客核心:

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

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', 
] 
0

self.template可能是空的,我想这是由于缓存......在任何情况下, 需要更换:

template = self.templates[0]['template'] 

template = None 
try: 
    template = self.templates[0]['template'] 
except IndexError: 
    pass 
相关问题