Django-debug-toolbar: Filtering by requested URL

Created on 29 May 2012  ·  3Comments  ·  Source: jazzband/django-debug-toolbar

I know that with the callback function one can implement his own complex mechanism of deciding when to display the Django Debug Toolbar and when not, but common filtering use cases (like checking users IP) should be made as simple as possible.

Therefore I suggest an configuration option to _filter out (not show) the toolbar on pages whose URLs match a specific pattern_ (or regular expression). E.g. if you are using the Django admin panel, but not developing enything in it, you do not need the debug toolbar there, so it would be nice to filter out the toolbar on all requests under '^admin/'. Same need appears if you have a page with many small iframes and you just want to debug the outside Django app and templates, not the ones in the small iframes - here it would be nice to hide the toolbar on all requests to the iframed app.

Suggestion is therefore to have one or more regular expressions for filtering where the toolbar should be shown based on the requested URL. E.g. either as a complex regex or as a list of simpler regexes:

DEBUG_TOOLBAR_CONFIG = ( ... 'HIDE_FILTER_URL': '^admin/|^myapp/someview', ... )
or
DEBUG_TOOLBAR_FILTER_URL = ('^admin/', '^myapp/someview')

All 3 comments

I stepped into the same issue and agree with gw0 on suggesting an implementation of common filtering patterns to quickly remove the toolbar from matching urls. Just an hint.

Either way, really good job the all-mighty debug-toolbar. You guys rock!

Now that #324 is fixed, this has become really simple to implement. Here's how you would exclude the admin:

from debug_toolbar.middleware import show_toolbar

def custom_show_toolbar(request):
    return show_toolbar(request) and not request.path.startswith('/admin/')

Here's how you would exclude a series of URL prefixes:

from debug_toolbar.middleware import show_toolbar

excluded_urls = ['/foo', '/bar']

def custom_show_toolbar(request):
    excluded = any(request.path.startswith(url) for url in excluded_urls)
    return show_toolbar(request) and not excluded

I don't think it would be easier to use a DSL for filtering URLs. Plain Python is a suitable tool for this class of problems.

Thanks for the suggestion anyway!

For anyone else landing here, it seems that the from debug_toolbar.middleware import show_toolbar line triggers an early evaluation of Django Debug Toolbars' settings, meaning that any subsequent changes to settings won't be read. I needed to do the import inside the function in order to have my settings take effect.

def custom_show_toolbar(request):
    from debug_toolbar.middleware import show_toolbar

    return show_toolbar(request) and not request.path.startswith("/graphql")

DEBUG_TOOLBAR_CONFIG = {"SHOW_TOOLBAR_CALLBACK": custom_show_toolbar}
Was this page helpful?
0 / 5 - 0 ratings