Campaign List
In this video, we are going to start writing views for campaign model. At first, we will proceed with the Campaign List View.
Resources
Code Setup
-
But at first, lets setup the project code for this section.
-
So, you need to download the project code files the resources section: DOWNLOAD ZIP FILE
-
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.
-
Run the migrations and create a superuser.
Admin App
- Create a 3-5 vaccination campaign from admin panel
Views
Now, open views.py file of campaign app and here we have to create a CampaignListView. In this entire section of videos, I will be using generic views.
# So, at first import the generic
from django.views import generic
# Then import the LoginRequiredMixin because we want only authenticated users to access all our campaign views.
from django.contrib.auth.mixins import LoginRequiredMixin
# Then import the Campaign model
from campaign.models import Campaign
# Now, create a CampaignListView.
# This view will inheirit LoginRequiredMixin because we want only authenticated users to access this view.
# Then we have to inherit the generic.ListView. Because we want to render the list of campaigns from this view
class CampaignListView(LoginRequiredMixin, generic.ListView):
# In this listview, at first we have to specify the model for which we are creating the list view.
model = Campaign
# Then specify the template name for this view.
template_name = "campaign/campaign/campaign-list.html"
# Now I want to paginate this view by 10 objects per page.
paginate_by = 10
# At last, I want to order the objects by their id in descending order.
ordering = ["-id"]
In this way, we have created a generic list view that renders the list of campaign objects.
Templates
Now, create templates folder inside campaign app. Then, create a sub-folder named campaign After that, create a file named "campaign-list.html
I have attched "campaign-list.html" file in the resources section. Please download it and copy its content and paste it here.
{% extends 'mysite/base.html' %}
{% block title %}
<title>Campaign 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-calendar"></i> Campaign List</h3>
<a href="#"><i class="fas fa-plus"></i> Create Campaign</a>
</div>
<table class="table table-responsive table-hover text-center">
<thead>
<tr>
<th scope="col">Vaccine</th>
<th scope="col">Center</th>
<th scope="col">Start Date</th>
<th scope="col">End Date</th>
<th scope="col">Select</th>
</tr>
</thead>
<tbody>
{% for campaign in object_list %}
<tr>
<td>{{ campaign.vaccine }}</td>
<td>{{ campaign.center.name }}</td>
<td>{{ campaign.start_date }}</td>
<td>{{ campaign.end_date }}</td>
<td><a href="#" class="btn-sm btn-outline-primary">View Details</a></td>
</tr>
{% endfor %}
</tbody>
</table>
{% include "components/pagination.html"%}
</div>
{% endblock content %}
URLS
Now, we have to create a urls.py file in the campaign app.
# In this file, at import import path
from django.urls import path
# Then import the views of campaign
from campaign import views
# At first, we have to specify the name of the app for which we have created this urls.py file
app_name = "campaign"
# Now, create the urlpatterns
urlpatterns = [
path("", views.CampaignListView.as_view(), name="campaign-list"),
]
Now, open urls.py of mysite.
# Here, we have to include our campaign.urls which we created in the campaign app.
path("campaign/", include("campaign.urls", namespace="campaign")),
[Run the development server and see the changes.]
In this way, we have create a campaign list view that renders a list of vaccination campaign.