Skip to content

Vaccination Certificate

In this video, we are going to create a feature to generate vaccination certificate once the vaccination of patient is completed and verified by the administrators.

Views

In the views.py file, lets create a function based view named vaccination certificate.

@login_required
def vaccination_certificate(request, vaccination_id):
    # At first, we will query the vaccination object from the database.
    vaccination = Vaccination.objects.get(id=vaccination_id)
    # Now, we will generate vaccination certificate only when the patient is vaccinated. 
    # We can confirm the vaccination status of patient from is_vacination value.
    if vaccination.is_vaccinated:
        # Here, we will create a context and pass this context to the generate_pdf function.
        context = {
            "pdf_title": f"{vaccination.patient.get_full_name() } | Vaccine Certificate",
            "date": str(timezone.now()),
            "title": "Vaccine Certificate",
            "subtitle": "To Whom It May Concern",
            "content": f"This is to certify that Mr/Ms/Mrs {vaccination.patient.get_full_name() } has successfuly taken {vaccination.campaign.vaccine.name } vaccine on {vaccination.date}. The vaccination was scheduled on { vaccination.slot.date } { vaccination.slot.start_time } at { vaccination.campaign.center.name }.",
        }
        return generate_pdf(context)
    else:
        return HttpResponseBadRequest("User Not Vaccinated")

URL

Now, lets create a url path to access this view.

path("vaccination-certificate/<int:vaccination_id>/", views.vaccine_certificate, name="vaccination-certificate"),

At last, add this url path in the vaccination-detail.html file.

        <button class="btn btn-sm btn-success m-1">
            <a href="{% url 'vaccination:vaccination-certificate' object.id %}"><i class="fas fa-download"></i> Vaccine Certificate</a>
        </button>

[Run the development server and see the changes]

[Commit the changes and push it in the remote repository.]