4. Configure an application¶

In the previous section of the tutorial, weadded a couple of applications. However, they were extremely simple andrequired very minimal configuration in the project’s settings.py andurls.py. In practice, adding a Django application to a project will requiremore complex configuration.

We’ll explore this by adding Django Debug Toolbar to the project.

Note

At this stage we will assume that you have your project running locally. SeeControl your project and see its console for a refresher on how to control your project.

For these steps, running your project with:

  1. docker-compose run --rm --service-ports web python manage.py runserver 0.0.0.0:80

is a good way of monitoring progress.

4.1. Add django-debug-toolbar to requirements.in¶

The Django Debug Toolbar installation notessuggest to install it using pip install django-debug-toolbar.

The latest stable version at the time of writing is 1.8, so adddjango-debug-toolbar==1.8 to requirements.in:

  1. # <INSTALLED_ADDONS> # Warning: text inside the INSTALLED_ADDONS tags is auto-generated. Manual changes will be overwritten.
  2. [...]
  3. # </INSTALLED_ADDONS>
  4. django-axes==2.3.2
  5. django-debug-toolbar==1.8

Run docker-compose build web to rebuild the project with the newrequirement.

4.2. Configure settings.py¶

We want Django Debug Toolbar to be active only when running with DEBUG =
True
, so all configuration for it will be conditional on an if DEBUG test.

4.2.1. Configure INSTALLED_APPS¶

Debug Toolbar requires django.contrib.staticfiles and debug_toolbar tobe present in INSTALLED_APPS. django.contrib.staticfiles is installedin Divio Cloud projects by default, so we just add debug_toolbar:

  1. if DEBUG:
  2.  
  3. INSTALLED_APPS.extend(["debug_toolbar"])

4.2.2. Configure Debug Toolbar settings¶

Normally, you’d set INTERNAL_IPS to ensure that this only runs on certain servers. With Docker, wedon’t always know what internal IP address a project will have, so we can’trely on that. However, relying on DEBUG will be enough, so we definea function that will serve as a SHOW_TOOLBAR_CALLBACK callback to replacethe default:

  1. if DEBUG:
  2.  
  3. [...]
  4.  
  5. def _show_toolbar(request):
  6. return True
  7. DEBUG_TOOLBAR_CONFIG = {"SHOW_TOOLBAR_CALLBACK": _show_toolbar}

4.2.3. Configure middleware settings¶

The installation documents note that we must set up the middleware, and that it should come as soon as possible in the list “after anyother middleware that encodes the response’s content, such as GZipMiddleware.”

So let’s insert it right after django.middleware.gzip.GZipMiddleware:

  1. if DEBUG:
  2.  
  3. [...]
  4.  
  5. MIDDLEWARE_CLASSES.insert(
  6. MIDDLEWARE_CLASSES.index("django.middleware.gzip.GZipMiddleware") + 1,
  7. "debug_toolbar.middleware.DebugToolbarMiddleware"
  8. )

4.3. Configure urls.py¶

Our approach in the urls.py is similar: we only want it active in DEBUGmode:

  1. from django.conf import settings
  2.  
  3. [...]
  4.  
  5. if settings.DEBUG:
  6. import debug_toolbar
  7. urlpatterns = [
  8. url(r'^__debug__/', include(debug_toolbar.urls)),
  9. ] + urlpatterns

4.4. See the results¶

And that’s it (Debug Toolbar has no database tables, so you don’t need to runmigrations).

Visit the admin to see the Debug Toolbar in action.
'Django Debug Toolbar'

原文: http://docs.divio.com/en/latest/introduction/04-addon-configuration.html