Django-debug-toolbar: Conflict with django_jinja

Created on 9 Aug 2016  路  6Comments  路  Source: jazzband/django-debug-toolbar

We upgraded from django-debug-toolbar 1.4 to 1.5 and ended up with an error that seems to break compatibility with django_jinja.

We received the same error as this Stack Overflow question:
http://stackoverflow.com/questions/38569760/django-debug-toolbar-template-object-has-no-attribute-engine

Line 175 in panels.templates.panel.py raises:

Exception Value: 'Template' object has no attribute 'engine'

If you look at the asker's written solution, you'll notice that they patched line 175 by checking for the engine attribute on self.templates[0]['template'] or to alternately look for a backend attribute (which exists and fulfills the same function in Jinja2).

This seems to have it's origin in the way that template dirs are grabbed, by pulling the engine info from an individual template:

        # Fetch context_processors/template_dirs from any template
        if self.templates:
            context_processors = self.templates[0]['context_processors']
            template_dirs = self.templates[0]['template'].engine.dirs
        else:
            context_processors = None
            template_dirs = []

In the following linked commit, you can see that debug_toolbar used to have a get_template_dirs() function that used django.template.engine.Engine.get_default() to get an engine and engine.dirs to pull in the template dirs. I haven't tested based on this commit, but I suspect that removing the call to django.template.engine.Engine.get_default() and pulling template dirs from an individual template caused the breaking change:
https://github.com/jazzband/django-debug-toolbar/commit/6390279a5eba1dc47fc12627204484fda67d3825#diff-f6bb86ee9199645f22f9b23d002ed707L24

The following seems to work and I would assume has a lower chance of breaking compatibility with other template engines.

from django.template.engine import Engine
engine = Engine.get_default()
engine.dirs # returns [] in my case

We encountered this using:

Django==1.10 (also tried with 1.9.9)
django_jinja==2.2.0
Jinja2==2.8
django-debug-toolbar==1.5

Most helpful comment

It looks like the only remaining thing blocking #876 from being merged is some tests to ensure that it will work with Django's bundled jinja2 backend, and to ensure that it will continue to work with django_jinja (to prevent future regression on this issue).

All 6 comments

I get this too, you can hack it fixed 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
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',
]

Hi guys,

Are there plans to resolve this?

There are no specific plans for the development of the debug toolbar. This will be fixed when someone writes a patch and someone else merges it.

PS: would you mind picking a better word that "guys" -- a synonym for "men" -- in order not to exclude half of humanity from the discussion? Thanks!

I'd be the first to criticise Tom but on this this occasion I'm afraid he's guilty of a fairly innocuous colloquialisms rather than insulting half of humanity, http://dictionary.cambridge.org/dictionary/english/guy:

guys: used to address a group of people of either sex

I've submitted a PR based on @stevelacey's comment.

I'm aware that in some parts of the US "guys" is sometimes used to address a group regardless of gender, but we also have contributors outside of the US :-) You left out the "mainly US" from your quote. Anyway I find it best to err on the side of caution.

It looks like the only remaining thing blocking #876 from being merged is some tests to ensure that it will work with Django's bundled jinja2 backend, and to ensure that it will continue to work with django_jinja (to prevent future regression on this issue).

Was this page helpful?
0 / 5 - 0 ratings

Related issues

tim-schilling picture tim-schilling  路  7Comments

clarkbarz picture clarkbarz  路  8Comments

NomadDemon picture NomadDemon  路  9Comments

akajb84 picture akajb84  路  7Comments

gw0 picture gw0  路  3Comments