.. _bootstrap-chapter: .. note:: A newer version of this tutorial using Django 1.9 is available from `Leanpub: https://leanpub.com/tangowithdjango19 `_ Bootstrapping Rango =================== In this chapter, we will be styling Rango using the *Twitter Bootstrap 3.2* toolkit. We won't go into the details about how Bootstrap works, and we will be assuming you have some familiarity with CSS. If you don't, check out the CSS chapter so that you understand the basics and then check out some Bootstrap tutorials. However, you should be able to go through this section and piece things together. To get started take a look at the `Bootstrap 3.2.0 website `_ - it provides you with sample code and examples of the different components and how to style them by added in the appropriate style tags, etc. On the Bootstrap website they provide a number of `example layouts `_ which we can base our design on. To style Rango we have identified that the `dashboard style `_ more or less meets our needs in terms of the layout of Rango, i.e. it has a menu bar at the top, a side bar (which we will use to show categories) and a main content pane. Given the html from the bootstrap website we need to do a bit of work on it before we can use it. This is what we did, the resulting html, which you can copy is below: * Replaced all references of ``../../`` to be ``http://getbootstrap.com`` * Replaced ``dashboard.css`` with the absolute reference, ``http://getbootstrap.com/examples/dashboard/dashboard.css`` * Removed the search form from the top nav bar * Stripped out all the non-essential content from the html and replaced it with ``{% block body_block %}{% endblock %}`` * Set the title element to be, ``Rango - {% block title %}How to Tango with Django!{% endblock %}`` * Changed the project name to be ``Rango``. * Added the links to the index page, login, register, etc to the top nav bar. * Added in a side block, i.e., ``{% block side_block %}{% endblock %}`` The New Base Template --------------------- .. code-block:: html Rango - {% block title %}How to Tango with Django!{% endblock %}
{% block body_block %}{% endblock %}
If you take a close look at the dashboard html source, you'll notice it has a lot of structure in it created by a series of ``
`` tags. Essentially the page is broken into two parts - the top navigation bar and the main pane which are denoted by the two ``
`` tags. In the nav bar section, we have injected all the links to the different parts of our website. Inside the main pane, there are two columns, one for the ``side_block``, and the other for the ``body_block``. Quick Style Change ------------------ Update your ``base.html`` with the html code above (assumes you are using the django-registration-redux package, if not you will need to update those url template tags). Reload your application. Obviously you will need a connection to the internet in order to download the css, js, and other related files. You should notice that your application looks heaps better with this one changes. Flip through the different pages. Since they all inherit from base, they will all be looking pretty good. Not perfect, but pretty good. .. note:: You could download all the associated files and stored them in your static folder. If you do this, simply update the base template to reference the static files stored locally. Now that we have the ``base.html`` all set up and ready to go, we can do a really quick face lift to Rango by going through the Bootstrap components and selecting the ones that suit the pages. Lets update the ``about.html`` template, by putting a page header on the page (http://getbootstrap.com/components/#page-header). From the example, all we need to do is provide an encapsulating ``
`` with the ``class="page-header"``: .. code-block:: html {% extends 'base.html' %} {% load staticfiles %} {% block title %}About{% endblock %} {% block body_block %}

.

Picture of Rango
{% endblock %} .. _fig-about-page-before: .. figure:: ../images/ch4-rango-about.png :figclass: align-center A screenshot of the About page without style. #TODO(leifos):update this screen shot. .. _fig-about-page-after: .. figure:: ../images/ch11-bootstrap-about.png :figclass: align-center A screenshot of the About page with Bootstrap Styling applied. #TODO(leifos):update this screen shot. To each template, add in a page-header. Remember to update all the templates in both ``rango`` and ``registration``. While the application looks much better, somethings look particularly out of place. For example on the registration page, the fields are not lined up, and the button looks like it is from the 20th century. .. _fig-register-initial: .. figure:: ../images/ch11-bootstrap-register-initial.png :figclass: align-center A screenshot of the Registration page with Bootstrap Styling applied but not customised. #TODO(leifos):update this screen shot. Also, you'll probably have noticed the sidebar is empty. In the next chapter we will sort that out with some handy navigation links. But first, lets sort out the Index page. The Index Page .............. Since we have only encapsulated the title with a page header i.e. ``