11

I am trying to use django-debug-toolbar with my django application and it worked for django v1.5. However, I am trying to migrate the system to django v1.6 and it is generating the following error when I try to load any page.

python manage.py runserver
Validating models...

0 errors found
December 21, 2013 - 22:53:18
Django version 1.6.1, using settings 'MySite.settings'
Starting development server at http://XXX.XXX.XXX.XXX:XXXX/
Quit the server with CONTROL-C.
Internal Server Error: /
Traceback (most recent call last):
  File "/home/user/django-env/local/lib/python2.7/site-packages/django/core/handlers/base.py", line 90, in get_response
response = middleware_method(request)
  File "/home/user/django-env/local/lib/python2.7/site-packages/debug_toolbar/middleware.py", line 45, in process_request
mod_path, func_name = func_path.rsplit('.', 1)
AttributeError: 'function' object has no attribute 'rsplit'
[21/Dec/2013 22:53:21] "GET / HTTP/1.1" 500 58172

The source has this note, but I'm not sure what they mean precisely (import_by_path):

def process_request(self, request):
    # Decide whether the toolbar is active for this request.
    func_path = dt_settings.CONFIG['SHOW_TOOLBAR_CALLBACK']
    # Replace this with import_by_path in Django >= 1.6.
    mod_path, func_name = func_path.rsplit('.', 1)
    show_toolbar = getattr(import_module(mod_path), func_name)
    if not show_toolbar(request):
        return

Also, while we're at it. Any idea what this message means as well?

/home/user/django-env/local/lib/python2.7/site-packages/debug_toolbar/settings.py:68: DeprecationWarning: SHOW_TOOLBAR_CALLBACK is now a dotted path. Update your DEBUG_TOOLBAR_CONFIG setting.
  "DEBUG_TOOLBAR_CONFIG setting.", DeprecationWarning)

My settings.py:

def custom_show_toolbar(request):
    return True  # Always show toolbar, for example purposes only.

DEBUG_TOOLBAR_CONFIG = {
    'INTERCEPT_REDIRECTS': False,
    'SHOW_TOOLBAR_CALLBACK': custom_show_toolbar,
    'INSERT_BEFORE': 'div',
    'ENABLE_STACKTRACES' : True,
}
James
  • 2,488
  • 2
  • 28
  • 45

1 Answers1

22

The problem is described by the message you get (which should not be a deprecation warning, rather it should be a TypeError, that's probably a bug in debug_toolbar):

SHOW_TOOLBAR_CALLBACK is now a dotted path. Update your DEBUG_TOOLBAR_CONFIG setting

The SHOW_TOOLBAR_CALLBACK setting used to be a callable, but now it is a dotted path to a callable: a string. The code you quoted tries to rsplit the value of SHOW_TOOLBAR_CALLBACK, but because you can't rsplit a function object, you get an error.

To solve this, put your callback into another python file, for example your_site/toolbar_stuff.py, and adjust the setting to 'SHOW_TOOLBAR_CALLBACK': 'your_site.toolbar_stuff.custom_show_toolbar'. You can test if this works beforehand by trying in the shell:

from your_site.toolbar_stuff import custom_show_toolbar

If that works, your new setting should work, too.

sk1p
  • 6,645
  • 30
  • 35
  • 3
    Hmm... I actually tried some of the things you suggested prior to posting--I looked at the documentation, but it wasn't very clear to me, the error isn't clear, and the comment in the code was misleading. I also tried to use a dotted path to my MySite.settings.custom_show_toolbar but that didn't work. The final solution is to use a dotted path, but to encapsulate it as a string. E.g., `'SHOW_TOOLBAR_CALLBACK': 'MySite.settings.custom_show_toolbar'`. Or, you can move the function to an external config file, but I kept it in settings. Thanks for the feedback it led me to the right solution! – James Dec 22 '13 at 19:39