Django-debug toolbar will not display from production server

I would like to see the Django Debug toolbar when accessing my website running Django 1.6. Debian 7.8, Nginx 1.2.1 and Gunicorn 19.1.1 are running on my server. However, when I try to access the site after adding DDT to my installed applications, 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 that it is not recommended to run the toolbar during production, but I just want to run it while I run some tests on my production server before opening it for general use. As you would expect, it works fine in my development environment on my laptop. I did some research and made sure to use the โ€œexplicitโ€ setting as recommended. I also ran the command "django-admin.py collectstatic" to make sure the static toolbar files were compiled into my STATIC_ROOT.

Since I'm running after a proxy server, I also added some middleware to ensure that the client IP address is passed to the toolbar middleware instead of my proxy IP address. This also did not fix the problem.

I show all the settings that apply to this problem below. Is there anything else I am missing?

Thanks!

These are the appropriate basic 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 are added to the basic settings during the production process:

 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 some 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 
+5
source share
4 answers

Actually, you should not install DEBUG on True on the production server, save it False and check my solution below:

The default DDT callback check ( debug_toolbar.middleware.show_toolbar ) is that DEBUG must be set to True , the request IP address must be in INTERNAL_IPS , and the request should not be an AJAX request.

We can provide our own callback that excludes the DEBUG configuration condition:

Parameters:

 INTERNAL_IPS = ['YOUR.IP.ADDRESS.HERE'] # put your client IP address here (not server IP!) DEBUG_TOOLBAR_CONFIG = { 'SHOW_TOOLBAR_CALLBACK': lambda request: not request.is_ajax() and request.META.get('REMOTE_ADDR', None) in INTERNAL_IPS } 

You can check HTTP_X_FORWARDED_FOR if you want, it is up to you.

URLs:

 if 'debug_toolbar' in settings.INSTALLED_APPS: import debug_toolbar urlpatterns += [ url(r'^__debug__/', include(debug_toolbar.urls)), ] 
+4
source

I use the same setup as the OP, with a notable exception to run everything in a separate Docker container, which makes the IP address of each service difficult to predict.

Here's how you force the Django Debug toolbar to always show (use it only locally, not during production):

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

So django-debug-toolbar uses JavaScript to run. Have you confirmed that you have conflicting JS scripts affecting your setup? I had a hell of a time with one project using DjDT, and it was a back-to-top script that was interfering ...

In addition, I know that you have a lot of additional code to handle your proxy situation, but do you have it right out of the box to find out if this works on your server? I can create a new virtualenv, start from scratch and make sure that it works on your server, and then go on to add applications and additional configuration.

This is probably what you were thinking about, but I thought I would add them anyway, since your question did not get a lot of action.

Good luck.

0
source

I needed to add the following to the project url.py file to fix the problem. After that, the debug toolbar appears.

  from django.conf.urls import include from django.conf.urls import patterns from django.conf import settings if settings.DEBUG: import debug_toolbar urlpatterns += patterns('', url(r'^__debug__/', include(debug_toolbar.urls)), ) 
0
source

Source: https://habr.com/ru/post/1212236/


All Articles