Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Filtering by requested URL #284

Closed
gw0 opened this issue May 29, 2012 · 3 comments
Closed

Filtering by requested URL #284

gw0 opened this issue May 29, 2012 · 3 comments

Comments

@gw0
Copy link

gw0 commented May 29, 2012

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')
@steppo40
Copy link

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!

@aaugustin
Copy link
Contributor

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!

@craiga
Copy link

craiga commented Feb 7, 2020

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}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants