.. _model-using-label: .. note:: A newer version of this tutorial using Django 1.9 is available from `Leanpub: https://leanpub.com/tangowithdjango19 `_ Models, Templates and Views =========================== Now that we have the models set up and populated with some data, we can now start putting things together. We'll be figuring out how to access data from the models within the views, and how to present this data via the templates. Basic Workflow: Data Driven Pages --------------------------------- There are five main steps that you must undertake to create a data driven webpage in Django. #. First, import the models you wish to use into your application's ``views.py`` file. #. Within the view you wish to use, query the model to get the data you want to present. #. Pass the results from your model into the template's context. #. Setup your template to present the data to the user in whatever way you wish. #. If you have not done so already, map a URL to your view. These steps highlight how Django's framework separates the concerns between models, views and templates. Showing Categories on Rango's Homepage -------------------------------------- One of the requirements regarding the main pages was to show the top five rango'ed categories. Importing Required Models ......................... To fulfil this requirement, we will go through each of the above steps. First, open ``rango/views.py`` and import the ``Category`` model from Rango's ``models.py`` file. .. code-block:: python # Import the Category model from rango.models import Category Modifying the Index View ........................ With the first step out of the way, we then want to modify our ``index()`` function. If we cast our minds back, we should remember the ``index()`` function is responsible for the main page view. Modify the function to look like the example below. .. code-block:: python def index(request): # Query the database for a list of ALL categories currently stored. # Order the categories by no. likes in descending order. # Retrieve the top 5 only - or all if less than 5. # Place the list in our context_dict dictionary which will be passed to the template engine. category_list = Category.objects.order_by('-likes')[:5] context_dict = {'categories': category_list} # Render the response and send it back! return render(request, 'rango/index.html', context_dict) Here we have performed steps two and three in one go. First, we queried the ``Category`` model to retrieve the top five categories. Here we used the ``order_by()`` method to sort by the number of likes in descending order - hence the inclusion of the ``-``. We then restricted this list to the first 5 ``Category`` objects in the list. With the query complete, we passed a reference to the list (stored as variable ``category_list``) to the dictionary, ``context_dict``. This dictionary is then passed as part of the context for the template engine in the ``render()`` call. .. warning:: Note that the Category Model contains the field ``likes``. So for this to work you need to have completed the exercises in the previous chapter, i.e. the Category Model needs to be updated to include the ``likes`` field. Modifying the Index Template ............................ With the view updated, all that is left for us to do is update the template ``rango/index.html``, located within your project's ``templates`` directory. Change the HTML code of the file so that it looks like the example shown below. .. code-block:: html Rango

Rango says...hello world!

{% if categories %} {% else %} There are no categories present. {% endif %} About Here, we make use of Django's template language to present the data using ``if`` and ``for`` control statements. Within the ```` of the page, we test to see if ``categories`` - the name of the context variable containing our list - actually contains any categories (i.e. ``{% if categories %}``). If so, we proceed to construct an unordered HTML list (within the ``