Center List
Resources:
Setup
In this video, we are going to create list view for center model.
First of all, download the Section 7 Project Code files from the resources section.
-
Now, extract the folder on the desktop.
-
Then, copy the mysite folder which is the project root directory.
-
Replace the mysite folder of your project with the copied mysite folder. So, if you made any mistake while writing the code in the previous section, then, it won't affect your learning in this section.
-
Open your project in the VS code and in the terminal, activate the virtual environment, and move to the mysite.
-
Then, migrate the changes to the SQLite database.
- Now, create a superuser having username "admin" and password "abde@12345".
Now, are are ready to go. At first, we need to create two center object from the Django admin panel. So, open your admin panel and create center objects.
Center 1:
name = "ABC Hospital"
address = "XYZ City, Somewhere, Earth"
Center 2:
name = "MNOP Hospital"
address = "UVW City, Somewhere, Earth"
Creating View
First of all, open views.py file from center app. Here, we will create a function based view named "center_list".
def center_list(request):
This function takes request as an argument.
In this view, first of all, we need to fetch all the center data from the database. So import the center model.
from center.models import Center
Now, create a variable named objects, and write Center.objects.all()
objects = Center.objects.all()
This django orm query retrieves all instances of the Center model from the database and assigns them to the variable objects.
Now create a dictionary named context. This context will store all the data that is needed to be passed in the django template.
context = {
"center": objects,
}
After that we need to render HTML file from this view. So, first of all, create a template folder in the center app and then create a sub folder named center. Then, create a center-list.html file. Now, in the center view, we will render this template.
return render(request, "center/center-list.html", context)
This line of code, will pass our context data to the center-list.html file and then render it.
After that, open "center-list.html" file. In this file, we have access to all our context data which we passed from our django view.
Therefore, lets use that data to display the center on our HTML page.
So, in this HTML file, we will create a for loop
{% for object in center %}
{% endfor %}
This is how you have to create a for loop in Django Templates. Here we are iterating over center which we passed as a key in context data from the center-list view.
After that, we will display the each instance of center data.
Name: {{ object.name }} <br>
Address: {{ object.address }} <br>
<hr>
This is how, you can display the variable data in the django template engine.
[Complete Code]
from center.models import Center
def center_list(request):
objects = Center.objects.all()
context = {
"center": objects,
}
return render(request, "center/center-list.html", context)
URL Routing
Now, currently, we haven't configured the url route for this view. So, first of all, create urls.py file in the center app.
First of all, import the path from django.urls.
from django.urls import path
Now, import the center view.
from center import views
Now, create a variable named app_name and set its value to "center".
app_name = "center"
Then, create a urlpatterns list and inside the list create one path for center list view.
urlpatterns = [
path("", views.center_list, name="list"),
]
Till now, we have just created the urls.py file in center app. But we haven't configured this center urls our root url so, django won't be able to discover this file.
To fix that, open urls.py file of mysite. Import the include function from django.urls
from django.urls import path, include
Now, create a new path in the urlpatterns.
path("center/", include("center.urls"))
After writing this path, whenver we write "center/" django will route that request to center.urls file.
In the center.urls file we have created urlpatterns, so django will search the path in it and route the request to the appropriate views.
In this path, we also, need to mention the namespace.
path("center/", include("center.urls", namespace="center"))
Now, one thing you might have noticed in this urls.py file in center app. We have wrote app_name in center urls.py file and in the root url file we are writing namespace="center".
But are writing this two things to reverse the URLs. That means in the coming videos, we will use some functions like reverse, reverse_lazy to navigate to a specific view using the url path name. If you are still confused on it, don't worry. You will understand this namespace in the upcoming videos.
Now, run the development server and see the changes.
[center.urls.py: Complete Code]
from django.urls import path
from center import views
app_name = "center"
urlpatterns = [
path("", views.center_list, name="list"),
]
Integrating Template
-
Finally, lets make the center list page more attractive and beautiful. So, for that, I have attached a HTML file for center list in the resources section.
-
Download that file and copy & paste the content of that file in the center-list.html file.
{% extends 'mysite/base.html' %}
{% block title %}
<title>Center List</title>
{% endblock title %}
{% block content %}
<div class="mt-3">
<div class="d-flex align-items-center justify-content-between">
<h3 class="text-center">
<i class="fas fa-map-marker-alt"></i> Center List
</h3>
<a href="#"><i class="fas fa-plus"></i> Create Center</a>
</div>
<table class="table table-responsive table-hover">
<thead>
<tr>
<th scope="col">Center Name</th>
<th scope="col">Select</th>
</tr>
</thead>
<tbody>
{% for object in center %}
<tr>
<td>{{object.name|upper}}</td>
<td><a href="#" class="btn-sm btn-outline-primary">View Details</a></td>
</tr>
{% endfor %}
</tbody>
</table>
</div>
{% endblock content %}
[Run the development server and see the changes.]
Now, this page looks much more better.
In this html file,
- First of all, we are extending the base template.
- Then, we are extending the title block to add title in the page.
- Then, in the block content, we have created a table to display list of all the center.
- In the center body, we are iterating over center which we passed as context data from our django view.
- We have also used a template tag named upper to uppercase the object name.
In this way, we have learnt how to create a list view and render all the list data using django template.