2016-07-25 17 views
1

新しいコンピュータで既存のDjangoプロジェクトを実行しようとしましたが、django-debug-toolbarに問題があります。それはJinja2と関係があるようです。ここでは、スタックトレースです:django-debug-toolbar: 'テンプレート'オブジェクトに 'engine'属性がありません

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' 

私は私のプロジェクトにJinja2のを統合するジャンゴ - Jinja2のを使っていますが、これは前に大丈夫働いたが、それは今、通常のDjangoテンプレートであることを、このtemplate変数を期待しているようです。私のTEMPLATESの設定では、Jinja2とDjangoTemplatesの両方が設定されています.Jinja2は特定の拡張子( 'tmpl')を使用してJinja2で使用されているテンプレートのみを使用し、その他はDjangoTemplatesバックエンドを通過できます。

Jinja2でdjangoデバッグツールバーを使用するとき、誰もこのエラーを見たことがありますか?私は必要に応じてさらに設定を投稿できます。

EDIT:要求されたとして、ここに私のテンプレート設定があります:

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を変更しますFrom:

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 = None 
try: 
    template = self.templates[0]['template'] 
except IndexError: 
    pass 

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

関連する問題