django-debug-toolbar won't display from production server
Question: I'd like to view the Django Debug Toolbar when accessing my production website
which is running Django 1.6. My server is running Debian 7.8, Nginx 1.2.1, and
Gunicorn 19.1.1. However, when I try to access the site after adding DDT to my
installed apps, I get the following error:

    
    
    NoReverseMatch at /
    u'djdt' is not a registered namespace
    Exception Location: /home/mysite/venv/mysite/local/lib/python2.7/site-packages/django/core/urlresolvers.py in reverse, line 505
    
    Error during template rendering
    In template /home/mysite/venv/mysite/local/lib/python2.7/site-packages/debug_toolbar/templates/debug_toolbar/base.html, error at line 12
    
    data-store-id="{{ toolbar.store_id }}" data-render-panel-url="{% url 'djdt:render_panel' %}"
    

I know it's not recommended that you run the toolbar in production but I just
want to run it while I do some testing on my production server prior to
opening it up for public use. As you might expect, it works just fine in my
development environment on my laptop. I did some research and have ensured
that I'm using the ["explicit" setup](http://django-debug-
toolbar.readthedocs.org/en/1.0/installation.html#explicit-setup) as
recommended [here](https://github.com/django-debug-toolbar/django-debug-
toolbar/issues/529). I also ran the command "django-admin.py collectstatic" to
ensure the toolbar's static files were collected into my STATIC_ROOT.

Since I'm running behind a proxy server, I also added some middleware to
ensure that the client's IP address is being passed to the toolbar's
middleware instead of my proxy's IP address. That didn't fix the problem
either.

I'm showing all the settings which seem pertinent to this problem below. Is
there something else I'm missing?

Thanks!

These are the pertinent base settings:

    
    
    SETTINGS_ROOT = os.path.abspath(os.path.dirname(__file__).decode('utf-8'))
    STATIC_ROOT = '/var/www/mysite/static/'
    STATIC_URL = '/static/'
    STATICFILES_DIRS = (
        os.path.join(SETTINGS_ROOT, "../../static"),
    )
    STATICFILES_FINDERS = (
        'django.contrib.staticfiles.finders.FileSystemFinder',
        'django.contrib.staticfiles.finders.AppDirectoriesFinder',
    )
    TEMPLATE_LOADERS = (
        'django.template.loaders.filesystem.Loader',
        'django.template.loaders.app_directories.Loader',
    )
    MIDDLEWARE_CLASSES = (
        'django.middleware.common.CommonMiddleware',
        'django.middleware.common.BrokenLinkEmailsMiddleware',
        'django.contrib.sessions.middleware.SessionMiddleware',
        'django.middleware.csrf.CsrfViewMiddleware',
        'django.contrib.auth.middleware.AuthenticationMiddleware',
        'django.contrib.messages.middleware.MessageMiddleware',
        'django.middleware.clickjacking.XFrameOptionsMiddleware',
    )
    TEMPLATE_DIRS = (
        os.path.join(SETTINGS_ROOT, "../../templates"),
    )
    INSTALLED_APPS = (
        'django.contrib.auth',
        'django.contrib.contenttypes',
        'django.contrib.sessions',
        'django.contrib.sites',
        'django.contrib.messages',
        'django.contrib.staticfiles',
        # Django management commands in 'scripts'
        'scripts',
        'apps.account',
    )
    

These production-only settings get added to base settings in production:

    
    
    DEBUG = True  # DDT needs this to be True
    TEMPLATE_DEBUG = DEBUG
    INSTALLED_APPS += (
        'django_extensions',
        # I'm using Django 1.6
        'debug_toolbar',  
    )
    if 'debug_toolbar' in INSTALLED_APPS:
    MIDDLEWARE_CLASSES += ('conf.middleware.DjangoDebugToolbarFix', 
                           'debug_toolbar.middleware.DebugToolbarMiddleware', )
    
        # I had to add this next setting after upgrading my OS to Mavericks
        DEBUG_TOOLBAR_PATCH_SETTINGS = False
        # IP for laptop and external IP needed by DDT
        INTERNAL_IPS = ('76.123.67.152', )
        DEBUG_TOOLBAR_CONFIG = {
           'DISABLE_PANELS': [
               'debug_toolbar.panels.redirects.RedirectsPanel',
            ],
           'SHOW_TEMPLATE_CONTEXT': True,
           'INTERCEPT_REDIRECTS': False
        }
    

This is in my urls.py:

    
    
    if 'debug_toolbar' in dev.INSTALLED_APPS:
        import debug_toolbar
        urlpatterns += patterns('',
            url(r'^__debug__/', include(debug_toolbar.urls)),
        )
    

Here is the additional middleware:

    
    
    class DjangoDebugToolbarFix(object):
        """Sets 'REMOTE_ADDR' based on 'HTTP_X_FORWARDED_FOR', if the latter is
        set."""
        def process_request(self, request):
            if 'HTTP_X_FORWARDED_FOR' in request.META:
                ip = request.META['HTTP_X_FORWARDED_FOR'].split(",")[0].strip()
                request.META['REMOTE_ADDR'] = ip
    


Answer: I am using the exact same setup as OP describes, with the noticeable exception
of running everything in a separate Docker container which make the IP of each
service hard to predict.

This is how you force Django Debug Toolbar to always show (only use this
locally, never in production):

    
    
    def custom_show_toolbar(request):
        return True # Always show toolbar, for example purposes only.
    
    DEBUG_TOOLBAR_CONFIG = {
        'SHOW_TOOLBAR_CALLBACK': custom_show_toolbar,
    }
    

