[Django]-How can django debug toolbar be set to work for just some users?

50👍

Try:

def show_toolbar(request):
    return not request.is_ajax() and request.user and request.user.username == "yourusername"

DEBUG_TOOLBAR_CONFIG = {
    'SHOW_TOOLBAR_CALLBACK': 'projectname.settings.show_toolbar',
    # Rest of config
}
👤Doug-W

11👍

The accepted answer is no longer correct. Newer versions of the toolbar need the value of the SHOW_TOOLBAR_CALLBACK key to be a string with the full import path of the function. So if you’re defining your callback function your settings.py file, you’d have to add:

DEBUG_TOOLBAR_CONFIG = {
    'SHOW_TOOLBAR_CALLBACK': 'projectname.settings.show_toolbar',
}

2👍

If you face No .rsplit() Error. NEW SOLUTION:

Because SHOW_TOOLBAR_CALLBACK is now a dotted string path and not support a callable.

edit your settings.py:

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

DEBUG_TOOLBAR_CONFIG = {
    'SHOW_TOOLBAR_CALLBACK': 'your_project_name.settings.custom_show_toolbar',
}

0👍

In Django 4+ this username validation worked for me:

def show_toolbar(request):
    return str(request.user) == "username" #... any other validations

Leave a comment