Skip to content

Center Detail

Resources:
- Center Detail HTML File

In this video, we are going to create detail view to view the center instance data.

View

First of all, we will create a view named center detail.

def center_detail(request, id):

This view takes request and id of center as an argument.

Now, lets write a query to fetch the center instance.

object = Center.objects.get(id=id)

Using this query, we will get the center instance.

After that, lets create a context dictionary.

context = {
        "center": object,
    }

We will pass this context data to our django template.

Now, at the end write

return render(request, "center/center-detail.html", context)

Currently, we haven't created center-detail page, so create it in the center folder of template inside the center app.

{{ center.name }}
{{ center.address }}

In this way, we have wrote a django view that takes id of center and render its details in HTML File.

[Complete Code]

def center_detail(request, id):
    object = Center.objects.get(id=id)
    context = {
        "center": object,
    }
    return render(request, "center/center-detail.html", context)

URL Routing

Now, lets configure the url for this view.

In the urls.py file of center, add a path.

path("", views.center_detail, name="detail"),

Now, our center detail view, takes an id as an argument, so we need to take id in the url.

path("<int:id>/", views.center_detail, name="detail"),

In this way, we have configured the url for center detail. Whenever Django matches to this url path, it will route the request to the center detail view and the name of this url is detail.

At last, open the center-list html file.

  • Navigate to the table body section.
  • You can find that the a tag is kept blank for the view details.
  • In this a tag, we will write the center detail url.
{% url '' %}

This is how, you can add a url in the django template.

'center:detail' object.id
  • When we write this URL, django will route our request to the detail view.
  • In this url, we have wrote center:detail.
  • Now, in the previous video, I discussed about namespace which we provided in the root url config file.
  • Using that namespace which we set as center, django knows where should I find the detail.
  • In the urls.py file of center, we have given the name of url path for center_detail as detail.
  • Therefore, when we write center:detail, then django will reference this path.
  • In this path, we are providing the id of center using object.id.

Now, run the development server and see the changes.

Template

  • Now, let us try to make the center detail page more attactive and beautiful.
  • So for that, I have attached center-detail html file in the resources section.
  • Download it, copy its content and paste it in the center-detail.html file.
{% extends 'mysite/base.html' %}

{% block title %} 
    <title>Center Detail</title>
{% endblock title %}

{% block content %}
<div class="card mb-3">
  <div class="card-body">
    <h5 class="card-title mb-4"><i class="fas fa-map-marker-alt"></i> Center 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">
        {{center.name}}
        </div>
    </div>

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

    <div class="row mb-1 btn-container">
      <div class="col-12">

        <button class="btn btn-sm btn-dark m-1">
          <a href="#">
            <i class="fas fa-eye"></i> View Storage
          </a>
        </button>

        <button class="btn btn-sm btn-success m-1">
          <a href="#">
            <i class="fas fa-pen"></i> Update Center
          </a>
        </button>

        <button class="btn btn-sm btn-danger m-1">
          <a href="#">
            <i class="fas fa-trash"></i> Delete Center
          </a>
        </button>
      </div>
    </div>
  </div>
</div>
{% endblock content %}

In this way, we have learnt how to list the objects and view details of that objects in HTML file by creating function based view.