32

Right away: yes I know about INTERNAL_IPS.

I'm about to have my django app opened up at work integration and testing. I know there will be debugging and plenty modifications and/or optimizations to be made so I'd love to have the Django Debug Toolbar. However, I'd prefer to not have it up for all of my co-workers (who are the 'clients').

The reason the INTERNAL_IP setting doens't work for just me (btw: I have a static IP on my development computer) is that I am using Nginx as a reverse-proxy and serving with Gunicorn. Because of the reverse-proxy, using an internal_ip of 127.0.0.1 shows DjDT to any computer on the network and using that ip is the only way I've been able to see it myself.

What I'm looking for is either a way to get my IP or my login name to be the only one to access the toolbar. I once saw a thread about the user name limited access but I can't find it...

And as a side question- anyone know why the toolbar doesn't render in IE? For me it just shows as tables on the bottom of the page.

j_syk
  • 6,511
  • 2
  • 39
  • 56

4 Answers4

49

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
}
Павел Тявин
  • 2,529
  • 4
  • 25
  • 32
Doug-W
  • 1,318
  • 12
  • 14
  • this works just fine, with the one change that it should be `request.user.username`. I got pretty hung-up on trying to find a way to add it via IP that I didn't really think about something like this and also didn't know about the callback setting. thanks – j_syk Jul 01 '11 at 14:53
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',
}
José Tomás Tocino
  • 9,873
  • 5
  • 44
  • 78
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',
}
hahakubile
  • 6,978
  • 4
  • 28
  • 18
0

In Django 4+ this username validation worked for me:

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