Django debugtoolbar for site running inside virtualbox/vagrant

Tags: django, apple

I had a hard time getting the Django debug toolbar to work this morning. Complicating factor: the site runs inside a vagrant virtualbox and my browser is simply running inside OSX.

(I use vagrant and virtualbox, see why I use vagrant and my vagrant setup on OSX)

Ok, I thought I had installed everything correctly. What can be the problem? I checked and double checked. The best way to verify your settings is to use django’s own “diffsettings” management command, that way you’re sure you’re looking at the right settings:

$ bin/django diffsettings
...
DEBUG = True
INSTALLED_APPS = ['debug_toolbar', 'lizard5_site', ... ]
INTERNAL_IPS = ('33.33.33.20', '127.0.0.1', '0.0.0.0')
...
MIDDLEWARE_CLASSES = ('debug_toolbar.middleware.DebugToolbarMiddleware', ...)
...

Debug mode is on, it is in the INSTALLED_APPS and I’ve correctly enabled the middleware. Oh, and I’ve adjusted the INTERNAL_IPS setting.

  • 127.0.0.1 even though I don’t really talk to the site from within my vagrant box.

  • 0.0.0.0 as that’s what I start my runserver with inside vagrant (bin/django runserver 0.0.0.0:8000): listen on everything on port 8000.

  • 33.33.33.20 as that’s the default IP address that vagrant/virtualbox gives me to talk to the virtualbox from within OSX. So on my laptop you’ll normally see http://33.33.33.20:8000/ instead of http://localhost:8000/ :-)

The 0.0.0.0 shouldn’t be needed, but at that time I was just trying things out to no avail.

… Time to call in the artillery. We have the source code, so I looked up the debug toolbar version I was using and put an import pdb;pdb.set_trace() into the def show_toolbar(request) method inside debug_toolbar/middleware.py.

The first lines of that short function are:

if request.META.get('REMOTE_ADDR', None) not in settings.INTERNAL_IPS:
    return False

In went the pdb and I reloaded the homepage:

> /.../django_debug_toolbar-1.0.1-py2.7.egg/debug_toolbar/middleware.py(26)show_toolbar()
-> if request.META.get('REMOTE_ADDR', None) not in settings.INTERNAL_IPS:
(Pdb) request.META['REMOTE_ADDR']
'33.33.33.1'

Sure enough, the error was in my INTERNAL_IPS setting after all. REMOTE_ADDR on the request turned out to be 33.33.33.1! Even though I talk to it from OSX with 33.33.33.20. So there must be some internal virtualbox/vagrant trickery that does some mapping here.

So: if you use the django debug toolbar inside vagrant: make sure you’ve got the correct port in the INTERNAL_IPS setting!

 
vanrees.org logo

About me

My name is Reinout van Rees and I work a lot with Python (programming language) and Django (website framework). I live in The Netherlands and I'm happily married to Annie van Rees-Kooiman.

Weblog feeds

Most of my website content is in my weblog. You can keep up to date by subscribing to the automatic feeds (for instance with Google reader):