jazzband / django-debug-toolbar

A configurable set of panels that display various debug information about the current request/response.
https://django-debug-toolbar.readthedocs.io/
BSD 3-Clause "New" or "Revised" License
8.04k stars 1.04k forks source link

django-debug-toolbar fails with jinja2 #790

Closed megahoy closed 8 years ago

megahoy commented 8 years ago

'Environment' object has no attribute 'install_gettext_translations' /usr/local/lib/python2.7/dist-packages/django_jinja/backend.py in init, line 142

douglas commented 8 years ago

It is happening for me too.

megahoy commented 8 years ago

I decided to use Django Template Engine for Debug Toolbar and Admin templates, and use jinja for other templates. Here is my config:

TEMPLATES = [
    {
        "BACKEND": "django_jinja.backend.Jinja2",
        "APP_DIRS": True,
        "OPTIONS": {
            "match_extension": ".jinja.html",
            'context_processors': [
                # Insert your TEMPLATE_CONTEXT_PROCESSORS here or use this
                # list if you haven't customized them:
                'django.contrib.auth.context_processors.auth',
                'django.contrib.messages.context_processors.messages',
                'board.context_processors.global_settings',
                'django.core.context_processors.request',
            ],
        }
    },
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [
            # insert your TEMPLATE_DIRS here
        ],
        'APP_DIRS': True,
        'OPTIONS': {
            'context_processors': [
                # Insert your TEMPLATE_CONTEXT_PROCESSORS here or use this
                # list if you haven't customized them:
                'django.contrib.auth.context_processors.auth',
                'django.contrib.messages.context_processors.messages',
                'board.context_processors.global_settings',
                'django.core.context_processors.request',
            ],
        },
    },
]
megahoy commented 8 years ago

But it would be great if django-debug-toolbar will support jinja

aaugustin commented 8 years ago

Actually this ticket isn't about supporting Jinja2 (with the built-in support) but about supporting django-jinja (a third-party project that predates the built-in support, with a different philosophy).

douglas commented 8 years ago

Hello, @aaugustin ! Exactly =)

Here is the trackback that shows the problem:

127.0.0.1 - - [18/Dec/2015 14:27:21] "GET /?__debugger__=yes&cmd=resource&f=console.png HTTP/1.1" 200 -
127.0.0.1 - - [18/Dec/2015 14:27:27] "GET / HTTP/1.1" 302 -
Internal Server Error: /login/
Traceback (most recent call last):
  File "/Volumes/DADOS/Work/ambientes/siej3/lib/python3.5/site-packages/django/core/handlers/base.py", line 174, in get_response
    response = self.process_exception_by_middleware(e, request)
  File "/Volumes/DADOS/Work/ambientes/siej3/lib/python3.5/site-packages/django/core/handlers/base.py", line 172, in get_response
    response = response.render()
  File "/Volumes/DADOS/Work/ambientes/siej3/lib/python3.5/site-packages/django/template/response.py", line 160, in render
    self.content = self.rendered_content
  File "/Volumes/DADOS/Work/ambientes/siej3/lib/python3.5/site-packages/django/template/response.py", line 137, in rendered_content
    content = template.render(context, self._request)
  File "/Volumes/DADOS/Work/ambientes/siej3/lib/python3.5/site-packages/django_jinja/backend.py", line 86, in render
    context=context)
  File "/Volumes/DADOS/Work/ambientes/siej3/lib/python3.5/site-packages/django/dispatch/dispatcher.py", line 192, in send
    response = receiver(signal=self, sender=sender, **named)
  File "/Volumes/DADOS/Work/ambientes/siej3/lib/python3.5/site-packages/debug_toolbar/panels/templates/panel.py", line 106, in _store_template_info
    if (isinstance(template.name, six.string_types) and
AttributeError: 'Template' object has no attribute 'name'
127.0.0.1 - - [18/Dec/2015 14:27:27] "GET /login/?next=/ HTTP/1.1" 500 -
Traceback (most recent call last):
  File "/Volumes/DADOS/Work/ambientes/siej3/lib/python3.5/site-packages/django/contrib/staticfiles/handlers.py", line 63, in __call__
    return self.application(environ, start_response)
  File "/Volumes/DADOS/Work/ambientes/siej3/lib/python3.5/site-packages/whitenoise/base.py", line 119, in __call__
    return self.application(environ, start_response)
  File "/Volumes/DADOS/Work/ambientes/siej3/lib/python3.5/site-packages/django/core/handlers/wsgi.py", line 177, in __call__
    response = self.get_response(request)
  File "/Volumes/DADOS/Work/ambientes/siej3/lib/python3.5/site-packages/django/core/handlers/base.py", line 230, in get_response
    response = self.handle_uncaught_exception(request, resolver, sys.exc_info())
  File "/Volumes/DADOS/Work/ambientes/siej3/lib/python3.5/site-packages/django/core/handlers/base.py", line 289, in handle_uncaught_exception
    return debug.technical_500_response(request, *exc_info)
  File "/Volumes/DADOS/Work/ambientes/siej3/lib/python3.5/site-packages/django_extensions/management/technical_response.py", line 5, in null_technical_500_response
    six.reraise(exc_type, exc_value, tb)
  File "/Volumes/DADOS/Work/ambientes/siej3/lib/python3.5/site-packages/six.py", line 686, in reraise
    raise value
  File "/Volumes/DADOS/Work/ambientes/siej3/lib/python3.5/site-packages/django/core/handlers/base.py", line 174, in get_response
    response = self.process_exception_by_middleware(e, request)
  File "/Volumes/DADOS/Work/ambientes/siej3/lib/python3.5/site-packages/django/core/handlers/base.py", line 172, in get_response
    response = response.render()
  File "/Volumes/DADOS/Work/ambientes/siej3/lib/python3.5/site-packages/django/template/response.py", line 160, in render
    self.content = self.rendered_content
  File "/Volumes/DADOS/Work/ambientes/siej3/lib/python3.5/site-packages/django/template/response.py", line 137, in rendered_content
    content = template.render(context, self._request)
  File "/Volumes/DADOS/Work/ambientes/siej3/lib/python3.5/site-packages/django_jinja/backend.py", line 86, in render
    context=context)
  File "/Volumes/DADOS/Work/ambientes/siej3/lib/python3.5/site-packages/django/dispatch/dispatcher.py", line 192, in send
    response = receiver(signal=self, sender=sender, **named)
  File "/Volumes/DADOS/Work/ambientes/siej3/lib/python3.5/site-packages/debug_toolbar/panels/templates/panel.py", line 106, in _store_template_info
    if (isinstance(template.name, six.string_types) and
AttributeError: 'Template' object has no attribute 'name'

Does this issue in django-jinja has anything to do with this problem ?

https://github.com/niwinz/django-jinja/issues/151

Thanks for any help !

douglas commented 8 years ago

Maybe this could be a possible solution:

https://github.com/django-debug-toolbar/django-debug-toolbar/pull/791

Also, some info about using Try/Except instead of hasattr, to check the existence of the attribute:

http://stackoverflow.com/a/9748715

HTH =)

niwinz commented 8 years ago

This is now fixed in django-jinja 2.1.0, and this issue can be considered as fixed I guess.

alexpetralia commented 8 years ago

I'm using django-jinja 2.1.1. I installed django-debug-toolbar and added it to INSTALLED_APPS. I believe those are all the steps necessary to get the toolbar working (even with jinja2, as per niwinz's most recent comment)?

If so, I'm getting this error:

Error during template rendering

In template /home/alexpetralia/Projects/mbta_django/venv/local/lib/python2.7/site-packages/debug_toolbar/templates/debug_toolbar/base.html, error at line 1
Encountered unknown tag 'load'.
1   {% load i18n %}{% load static from staticfiles %}
2   <link rel="stylesheet" href="{% static 'debug_toolbar/css/print.css' %}" type="text/css" media="print" />
3   <link rel="stylesheet" href="{% static 'debug_toolbar/css/toolbar.css' %}" type="text/css" />
4   {% if toolbar.config.JQUERY_URL %}
5   <!-- Prevent our copy of jQuery from registering as an AMD module on sites that use RequireJS. -->
6   <script src="{% static 'debug_toolbar/js/jquery_pre.js' %}"></script>
7   <script src="{{ toolbar.config.JQUERY_URL }}"></script>
8   <script src="{% static 'debug_toolbar/js/jquery_post.js' %}"></script>
9   {% else %}
10  <script src="{% static 'debug_toolbar/js/jquery_existing.js' %}"></script>
11  {% endif %}

For reference (although I don't think it should make a difference?), here are my TEMPLATE settings:

TEMPLATES = [
    {
        'BACKEND': 'django_jinja.backend.Jinja2',
        'DIRS': ['templates'],
        'APP_DIRS': True,
        'OPTIONS': {
            'match_extension': '.html',
            'app_dirname': 'templates',
            "extensions": [
                "jinja2.ext.do",
                "jinja2.ext.loopcontrols",
                "jinja2.ext.with_",
                "jinja2.ext.i18n",
                "jinja2.ext.autoescape",
                "django_jinja.builtins.extensions.CsrfExtension",
                "django_jinja.builtins.extensions.CacheExtension",
                "django_jinja.builtins.extensions.TimezoneExtension",
                "django_jinja.builtins.extensions.UrlsExtension",
                "django_jinja.builtins.extensions.StaticFilesExtension",
                "django_jinja.builtins.extensions.DjangoFiltersExtension",
            ],
            "bytecode_cache": {
                "name": "default",
                "backend": "django_jinja.cache.BytecodeCache",
                "enabled": False,
            },
            "autoescape": True,
        },

    },     
]
niwinz commented 8 years ago

You have configured jinja2 backend for all html files, this means that jinja2 engines will be used for all html files, but django-debug-toolbar uses django template engine and jinja2 can't compile django templates... that causes your error.

It is highly recommended use different extensions (like .jinja) for files that uses jinja2 template engine.

aaugustin commented 8 years ago

Thanks @niwinz!

Since it doesn't appear that changes are required in the toolbar, I'm going to close this issue.

alexpetralia commented 8 years ago

Thank you so much! Sorry I misunderstood. I fixed my problem now thanks to your advice.

On Wed, Jan 20, 2016 at 4:34 PM, Aymeric Augustin notifications@github.com wrote:

Thanks @niwinz https://github.com/niwinz!

Since it doesn't appear that changes are required in the toolbar, I'm going to close this issue.

— Reply to this email directly or view it on GitHub https://github.com/django-debug-toolbar/django-debug-toolbar/issues/790#issuecomment-173366573 .