Set up data in Django view methods to access on Django templates

Problem

You want to set up data in a Django view method to access it on a Django template

Solution

Add a Python dictionary to the render method with the data you wish access on a Django template. The Python dictionary can include any Python data structure (e.g. list, tuple, dictionary). Inside the template use the dictionary keys to display values.

How it works

To set up data in a Django view method you first need to declare it or extract it inside the method body. You can declare strings, numbers, lists, tuples, dictionaries or any other Python data structure. And you can also extract data from a variety of sources, primarily from the database connected to a Django project, web services or social APIs.

Once you declare or extract the data inside a view method, you create a dictionary to make the data accessible on a Django template. The dictionary keys represent the reference names for the template, while the values are the data structures themselves. Listing 1 illustrates a view method that declares multiple data structures and passes them to a template.

Listing 1 - Set up dictionary in Django view method for access in template

from django.shortcuts import render


def detail(request,store_id=1,location=None):
    # Create fixed data structures to pass to template
    # data could equally come from database queries
    # web services or social APIs
    STORE_NAME = 'Downtown'
    store_address = {'street':'Main #385','city':'San Diego','state':'CA'}
    store_amenities = ['WiFi','A/C']
    store_menu = ((0,''),(1,'Drinks'),(2,'Food'))
    vals_for_template = {'store_name':STORE_NAME, 'store_address':store_address, 'store_amenities':store_amenities, 'store_menu':store_menu}
    return render(request,'stores/detail.html', vals_for_template)

Notice in listing 1 how the render method includes the vals_for_template dictionary. In previous recipes, the render method just included the request object and a template to redirect the request. In listing 1, a dictionary is passed as the last render argument. By specifing a dictionary as the last argument, the dictionary becomes available to the template -- which in this case is stores/detail.html

The dictionary in listing 1 contains keys strings and values that are the data structures declared in the method body. The dictionary keys become references to access the values inside a Django template. The dictionary values are then exposed inside a template for display on a web page. Listing 2 illustrates the Django template with the syntax to extract the dictionary values.

Listing 2 - Django template access values passed from Django view method

<h4>{{store_name}} store</h4>
<p>{{store_address.street}}</p>
<p>{{store_address.city}},{{store_address.state}}</p>
<hr/>
<p>We offer: {{store_amenities.0}} and {{store_amenities.1}}</p> 
<p>Menu includes : {{store_menu.1.1}} and {{store_menu.2.1}}</p> 

The template in listing 2 illustrates several access patterns to access the values from the dictionary in listing 1. The first declaration {{store_name}} uses the standalone key to display the Downtown value. The other access declarations use dot(.) notation because the values themselves are composite data structures.

The store_address key contains a dictionary, so to access the internal dictionary values you use the internal dictionary key separated by a dot(.). store_address.street displays the street value, store_address.city displays the city value and store_address.state displays the state value.

The store_amenities key contains a list which uses a similar dot(.) notation to access internal values. However, since Python lists don't have keys you use the list index number. store_amenities.0 displays the first item in list store_amenities and store_amenities.1 displays the second item in list store_amenities.

The store_menu key contains a tuple of tuples which also requires a number on account of the lack of keys. {{store_menu.1.1}} displays the second tuple value of the second tuple value of store_menu and {{store_menu.2.1}} displays the second tuple value of the third tuple of store_menu.