Delete Vaccine
Resources:
In this video, we are going to learn how to perform delete operation on Vaccine model using class based view.
View
At first, lets create a new view named "DeleteVaccine".
class DeleteVaccine(View):
After that create a template_name attribute.
template_name = "vaccine/delete-vaccine.html"
Then create delete-vaccine.html file in the vaccine folder of templates.
In the html file, we have to include the form component.
{% include "components/form.html" with form_name="Delete Vaccine" %}
In this delete-vaccine page, we will render empty form with csrf token. That means, this form will not have any model fields but it will contain a hidden field for csrf_token.
GET
Now, lets define the get method of this view.
def get(self, request, id):
This get method takes id as an additional argument. This id represents the vaccine id which the user wants to delete.
In this get method, at first, we have to query the vaccine instance.
vaccine = get_object_or_404(Vaccine, id=id)
Then, we need to create a context with this vaccine data.
context = {
"object": vaccine,
}
After that, we have to render the delete-vaccine page with this context.
return render(request, self.template_name, context)
URL
In order to access the view, we have to add a new url path in the urls.py file.
path("delete/<int:id>/", views.DeleteVaccine.as_view(), name="delete"),
After that, we need to add this url path in the vaccine detail html file.
<button class="btn btn-sm btn-danger m-1">
<a href="{% url 'vaccine:delete' object.id %}">
<i class="fas fa-trash"></i> Delete Vaccine
</a>
</button>
[Run the development server and see the changes.]
POST
Now, lets define the post method for the delete view.
def post(self, request, id):
This post method also takes id as an additional argument. This id represents the vaccine id which the user wants to delete.
In this post method, we will write a query to delete the vaccine object.
Vaccine.objects.filter(id=id).delete()
This is how, we can directly delete the vaccine instance without even querying it and saving in our python memory.
After deleting the vaccine instance, we have to redirect the user to the vaccine list page.
return HttpResponseRedirect(reverse("vaccine:list"))
[Run the development server and see the changes.]
[Complete Code]
class DeleteVaccine(View):
template_name = "vaccine/delete-vaccine.html"
def get(self, request, id):
vaccine = get_object_or_404(Vaccine, id=id)
context = {
"object": vaccine,
}
return render(request, self.template_name, context)
def post(self, request, id):
Vaccine.objects.filter(id=id).delete()
return HttpResponseRedirect(reverse("vaccine:list"))
Templates
Currently, our vaccine delete page looks much ugly. So, lets make it attractive and user friendly.
I have attached vaccine-delete file in the resources section. Download it, copy its content and paste it in the delete-vaccine.html file.
{% extends 'mysite/base.html' %}
{% block title %}
<title>Delete Vaccine</title>
{% endblock title %}
{% block content %}
<div class="card p-3">
<div class="card-text">
<h4>Are you sure you want to delete {{ object.name }} ?</h4>
{% include "components/form.html" with form_name="Delete Vaccine" %}
</div>
</div>
{% endblock content %}
Now our delete-vaccine page looks much more user-friendly. In this way, we have learnt how to handle the delete operation on any given model using class based views.
Git
Now add the changes to the staging area.
git add .
After that, commit the changes.
git commit -m "Added Vaccine CRUD Views"
Then push the changes to the remote repository.
git push origin main
[Commit the changes and push it in the remote repository.]