Skip to content

Update Vaccine

Resources:
- Update Vaccine HTML File

In this video, we are going to learn how to perform update operation on Vaccine model using class based view.

Views

First of all, create a view named UpdateVaccine and inherit the View.

class UpdateVaccine(View):

Now, in this class, create a form_class attribute and set it to VaccineForm.

form_class = VaccineForm

If there are multiple methods in this class, then in each of the method, we will access the form by writing self.form_class.

Similarly, create a template name attribute.

template_name = "vaccine/update-vaccine.html"

Here also, if there are multiple methods in this class, then in each of the method, we will access the template_name by writing self.template_name.

GET

At first, lets try to handle the get method of this view.

So, create a get method that takes id as an additional argument.

def get(self, request, id):

Then, at first we need to query the vaccine instance that matches the given id. Instead of using try except block, we will use a shortcut method called get_object_or_404.

from django.shortcuts import get_object_or_404

This method will either return an object or return 404 error if the object is not found. This is similar to what we did in the try except block in detail view. When we use this method, we don't need to worry about 404 error.

vaccine = get_object_or_404(Vaccine, id=id)

This method takes model name as the first argument and in the second argument, we need to specify the primary key.

After that we have to create a context and add the form.

context = {
    "form": self.form_class(instance = vaccine),
}

Now, render the template with this context.

return render(request, self.template_name, context)

Since, we haven't created the update-vaccine.html file, create it inside the vaccine folder.

Then include the form component in the html file.

{% include "components/form.html" with form_name="Update Vaccine" %}

In the way, we have created a get method for the update vaccine view that renders the form in the update-vaccine.html file.

URL Configuration.

Now, lets add a new url path for the update vaccine view. In the urls.py, add

path("update/<int:id>/", views.UpdateVaccine.as_view(), name="update"),

After that, add this url in the vaccine list page.

<button class="btn btn-sm btn-success m-1">
    <a href="{% url 'vaccine:update' object.id %}">
        <i class="fas fa-pen"></i> Update Vaccine
    </a>
</button>

[Run the development server and see the changes.]

POST method

Till now, we have only wrote the get method for this view. Now, lets add the post method.

def post(self, request, id):

In this post method, first of all, we need to query the vaccine instance.

vaccine = get_object_or_404(Vaccine, id=id)

Then, we will instantiate the form with request.POST data and vaccine instance.

form = self.form_class(request.POST, instance=vaccine)

After that, we will check whether the form is valid or not:

if form.is_valid():

If the form is valid, we will save the form.

form.save()

After saving the form, we have to redirect the user to the vaccine detail page.

return HttpResponseRedirect(reverse("vaccine:detail", kwargs={"id": id}))

If the form is not valid, we have to render the same form back to the user.

return render(request, self.template_name, {"form": form})

[Run the development server and see the changes.]

[Complete Code]

class UpdateVaccine(View):
    form_class = VaccineForm
    template_name = "vaccine/update-vaccine.html"

    def get(self, request, id):
        vaccine = get_object_or_404(Vaccine, id=id)
        context = {
            "form": self.form_class(instance = vaccine),
        }
        return render(request, self.template_name, context)

    def post(self, request, id):
        vaccine = get_object_or_404(Vaccine, id=id)
        form = self.form_class(request.POST, instance=vaccine)
        if form.is_valid():
            form.save()
            return HttpResponseRedirect(reverse("vaccine:detail", kwargs={"id": id}))
        return render(request, self.template_name, {"form": form})

Templates

Currently, our update vaccine page looks ugly. So, lets make it more attractive and user friendly.

I have attached update-vaccine.html file in the resources section. Download it, copy its content and paste it in the update-vaccine.html file.

{% extends 'mysite/base.html' %}

{% block title %} 
    <title>Update Vaccine</title>
{% endblock title %}

{% block content %}
<div class="card p-3">
  <div class="card-body">
    <h4 class="mb-3">Fill the following details to update the vaccine details.</h4>
    {% include "components/form.html" with form_name="Update Vaccine" %}
  </div>
</div>
{% endblock content %}

Now, the update-vaccine page looks much more better. In this way, we have learnt how to handle the update operation of any given model using class based view.