Skip to content

Storage Detail

In this video, we are going to learn about generic Detail View.

Generic DetailView is a class-based view that is used to display the details of a single object retrieved from a database. It provides a pre-built implementation that can handle common tasks related to retrieving and presenting the details of an individual object.

In this video, we will create a Detail View api for Storage model.

Resources:

View

First of all, create a class named "StorageDetail" and then inherit generic.DetailView.

class StorageDetail(generic.DetailView):

This is how you can create generic DetailView for Storage model

Now, you need to explicitly tell this class the model for which you are creating Detail view.

model = Storage

After that, specify the template name for the storage-detail page.

template_name = "storage/storage-detail.html"

Template

Then, create a storage-detail.html file in the storage folder of templates.

In the html file, we can access the storage information using object name.

{{ object.center.name }}
{{ object.vaccine.name }}
{{ object.total_quantity }}

In this way, we can render the storage object information in our html file.

URL

Now, add the url for this view.

path("storage/<int:pk>/", views.StorageDetail.as_view(), name="storage-detail"),

Also, add this url in the html file of storage-list.

<td><a href="{% url 'center:storage-detail' storage.id %}" class="btn-sm btn-outline-primary">View Details</a></td>

[Run the development server and see the changes.]

Template Modification

Now, lets modify the storage detail template and make it look better.

I have attached storage-detail.html file in the resources section. Download it, copy its content and paste it in the html file.

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

{% block title %} 
    <title>Storage Details</title> 
{% endblock title %}

{% block content %}
<div class="card mb-3">
  <div class="card-body">
    <h5 class="card-title mb-4"><i class="fas fa-box"></i> Storage Details</h5>

    <div class="row mb-1">
      <div class="col-5 col-sm-4 col-md-3 col-lg-2 fw-bold">
        Center Name
      </div>
      <div class="col-7 col-sm-8 col-md-9 col-lg-10">
        {{ object.center.name }}
      </div>
    </div>

    <div class="row mb-1">
      <div class="col-5 col-sm-4 col-md-3 col-lg-2 fw-bold">
        Vaccine Name
      </div>
      <div class="col-7 col-sm-8 col-md-9 col-lg-10">
        {{ object.vaccine.name }}
      </div>
    </div>

    <div class="row mb-1">
      <div class="col-5 col-sm-4 col-md-3 col-lg-2 fw-bold">
        Total Quantity
      </div>
      <div class="col-7 col-sm-8 col-md-9 col-lg-10">
        {{ object.total_quantity }}
      </div>
    </div>

    <div class="row mb-1">
      <div class="col-5 col-sm-4 col-md-3 col-lg-2 fw-bold">
        Booked Quantity
      </div>
      <div class="col-7 col-sm-8 col-md-9 col-lg-10">
        {{ object.booked_quantity }}
      </div>
    </div>

    <div class="row mb-1">
      <div class="col-5 col-sm-4 col-md-3 col-lg-2 fw-bold">
        Available Quantity
      </div>
      <div class="col-7 col-sm-8 col-md-9 col-lg-10">
        {{ available_quantity }}
      </div>
    </div>

    <div class="row mb-1 btn-container">
      <div class="col-12">
        <button class="btn btn-sm btn-success m-1">
          <a href="#">
            <i class="fas fa-pen"></i> Update Storage
          </a>
        </button>

        <button class="btn btn-sm btn-danger m-1">
          <a href="#">
            <i class="fas fa-trash"></i> Delete Storage
          </a>
        </button>
      </div>
    </div>

  </div>
</div>
{% endblock content %}

Now, our storage detail page looks much more better.

Adding context data to the view

[Open storage detail template and highlight available quantity section and then read this]

If you see our storage detail page, you can find that there is a field named "Available quantity". This field value is empty. It is because Storage model has no any available quantity field. This available quantity field is the substraction of total quantity and booked_quantity. So, what we will do, we will pass the value of available quantity from our Storage Detail view.

If you need to pass any extra data to your templates, you need to override get_context_data method.

def get_context_data(self, **kwargs):

At first, get the context of this view.

    context = super().get_context_data(**kwargs)

Now, add available quantity to this context.

    context["available_quantity"] = self.object.total_quantity - self.object.booked_quantity

At last, return the context

    return context

Now, open the storage detail page and you can see that the available quantity field is filled with the value.

In this way, we have learnt how to create generic DetailView for any given model in django.