Now that we have a basic application where we can add contacts and list them, it's reasonable to think about how we'd make this look more appealing. Most modern web applications are a combination of server side code/views, and client side, static assets, such as JavaScript and CSS. Regardless of whether you choose JavaScript or CoffeeScript, CSS or SASS, Django provides support for integrating static assets into your project.
Django distinguishes between "static" and "media" files. The former are static assets included with your app or project. The latter are files uploaded by users using one of the file storage backends. Django includes a contrib app, django.contrib.staticfiles for managing static files and, importantly, generating the URLs to them. You could, of course, simply hard code the URLs to your static assets, and that'd probably work for a while. But if you want to move your static assets to their own server, or to a CDN, using generated URLs let's you make that change without needing to update your templates. django.contrib.staticfiles is enabled by default when you create a new project, so you can just start using it.
We're going to add Bootstrap to our project for some basic styling. You can download the Bootstrap files from its website, http://getbootstrap.com.
Django supports adding static files at both the application and project level. Where you add them sort of depends on how tied to your specific assembly of apps they are. That is, are they reusable for anyone using your app, or are they specific to your particular deployment?
App specific static files are stored in the static subdirectory within the app. Django will also look in any directories listed in the STATICFILES_DIRS setting. Let's update our project settings to specify a static files directory.
importos.path...# Additional locations of static filesSTATICFILES_DIRS=(# Put strings here, like "/home/html/static" or "C:/www/django/static".# Always use forward slashes, even on Windows.# Don't forget to use absolute paths, not relative paths.os.path.join(os.path.dirname(__file__),'static',),)
Note that we use os.path to construct the absolute path. This ensures Django can locate the files unambiguously.
Let's go ahead and create the static directory in our project and unpack Bootstrap into it.
The Django staticfiles app includes a template tag that make it easy to refer to static files within your templates. You load template tag libraries using the load tag.
{% load staticfiles %}
After loading the static files library, you can refer to the file using the static tag.
Note that the path we specify is relative to the static files directory. Django is going to join this path with the STATIC_URL setting to generate the actual URL to use.
The STATIC_URL setting tells Django what the root URL for your static files is. By default it's set to /static/.
We want to add the Boostrap CSS to all of our templates, but we'd like to avoid repeating ourself: if we add it to each template individually, when we want to make changes (for example, to add another stylesheet) we have to make them to all the files. To solve this, we'll create a base template that the others will inherit from.
Let's create base.html in the templates directory of our contacts app.
We've told Django where we store our static files, and we've told it what URL structure to use, but we haven't actually connected the two together. Django doesn't serve static files by default, and for good reason: using an application server to serve static resources is going to be ineffecient, at best. The Django documentation on deploying static files does a good job of walking through the options for getting your static files onto your CDN or static file server.
For development, however, it's convenient to do it all with one process, so there's a helper. We'll update our addressbook/urls.py file to include the staticfiles_urlpatterns helper.