Django – Adding Item Details from Database

In my previous post I showed you how to build a new page in Django, now let’s take a step further and add more details from the database. Each Burger in our database has an associated list of toppings. We want to display those toppings for each burger.

We’ll follow a similar to process as last time, define the URL, create the view, make the page HTML. We’ll the modify the burgers page so each item in the list is a link to the specific toppings page for the individual burger. Let’s get started.

Step 1 – Modify urls.py

Update your app’s url.py file to add the following code under urlpatterns:

# Page that lists all toppings for a given burger
    path('burgers/<int:burger_id>/', views.burger, name='burger')

What is this code doing? In burgers/<int:burger_id>/ Django is looking for the word burgers after the base URL and an integer that matches burger_id. When it matches both these things, it calls the views.py function burger with burger_id.

Step 2 – Modify views.py

Add the following function to your views.py file:

def burger(request, burger_id):
    '''Return all toppings for a given burger ID'''
    burger = Burger.objects.get(id=burger_id)
    toppings = burger.topping_set.all()
    context = {'burger': burger, 'toppings': toppings}
    return render(request, 'burgers/burger.html', context)

This function takes burger_id from the page request and looks for a burger in the database matching that ID number. It then gets a list of all the toppings associated with that burger ID. Next, we build the context for the page, then finally return the context to a page called burger.html.

Step 3 – Create the burger.html File

Now, we create a page that displays details for an individual burger. Create a file called burger.html that looks like the following code.

{% extends 'burgers/base.html' %}

{% block content %}
<p>Burger: {{ burger }}</p>
<ul>
    {% for topping in toppings %}
        <li>{{ topping|linebreaks }}</li>
    {% empty %}
        <li>No toppings listed for this burger!</li>
    {% endfor %}
</ul>

{% endblock content %}

This page show the name of the burger, then a list of all the toppings. We use the name {% empty %} tag as before to signify what to show if the list is empty.

Step 4 – Update the Burgers Page

Now we need to update the burgers page to generate a link to the topping page for each burger. Make your burgers.html file match the following code.

{% extends 'burgers/base.html' %}

{% block content %}
    <p>Specialty Burgers:</p>
    <ul>
        {% for burger in burgers %}
            <li>
                <a href="{% url 'burgers:burger' burger.id %}">{{ burger }}</a>
            </li>
        {% empty %}
            <li>No special burgers on the menu!</li>
        {% endfor %}
    </ul>
{% endblock content %}

This code inserts a link with each burger name. It looks up burger.id in the database then generates a link to that ID.

Pay close attention to how you use burger.id and burger_id. They look similar, but burger.id looks for a burger and returns its database ID. burger_id is variable used to pass an ID number specified by the user or requesting page.

Now we’re finished! Save all your file and reload your webpage. You should see something like this when you click the Specialty Menu:

Then, clicking a burger will bring up its details:

We’ve now seen how to add more details to a Django page from the database. That concludes this Django tutorial. Stick around for some future Django projects!

John

Leave a Reply

Your email address will not be published. Required fields are marked *