My Vaccination
In this video, we are going to create my-vaccination page where users can see the list of vaccinations they have applied for.
Resources
Views
So, in the views.py file, let me create a new view named VaccinationList
class VaccinationList(LoginRequiredMixin, generic.ListView):
# At first, we have to specify the name of model for which we creating this model
model = Vaccination
# Now, give the name of template
template_name = "vaccination/vaccination-list-patient.html"
# After that paginate this view by 10 objects per page.
paginate_by = 10
# Then, specify the ordering of the queryset,
ordering = ["id"]
# By default this view will return all the vaccination list.
# But we have to show the vaccination list of only that user who is logged in.
# So, we have to override the queryset mand apply filter on it to filter vaccination by request.user
def get_queryset(self):
return Vaccination.objects.filter(patient=self.request.user)
Templates
Now, lets create a vaccination-list-patient.html file in the vaccination folder of templates. I have attached vaccination-list-patient.html file in the resources section. Please download it and paste its content in this file.
{% extends 'mysite/base.html' %}
{% block title %}
<title>Vaccination 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-syringe"></i> Your Vaccination History</h3>
</div>
<table class="table table-hover">
<thead>
<tr>
<th scope="col">Vaccine Name</th>
<th scope="col">Date</th>
<th scope="col">Status</th>
<th scope="col">Select</th>
</tr>
</thead>
<tbody>
{% for vaccination in object_list %}
<tr>
<td>{{ vaccination.campaign.vaccine.name|upper }}</a></td>
<td>{{ vaccination.slot.date }}</td>
<td>
{% if vaccination.is_vaccinated %}
<button class="btn btn-sm btn-success">Vaccinated</button>
{% else %}
<button class="btn btn-sm btn-primary">Scheduled</button>
{% endif %}
</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
At last, lets create a url path to access this view.
path("", views.VaccinationList.as_view(), name="vaccination-list-patient"),
After that, we have to add this url in the dashboard.html
<a href="{% url 'vaccination:vaccination-list-patient' %}" class="dashboard-card-link">
<i class="fas fa-book-medical fs-1"></i> <br>
My <br> Vaccination
</a>
[Run the development server and see the changes]