Slot Delete
In this video, we aree going to create slot delete page. Using this page, the user can delete the slot for a particular vaccination campaign.
Resources
Views
In the views.py file, let us create SlotDeleteView.
# This view will inherit LoginRequiredMixin, PermissionRequiredMixin, SuccessMessageMixin, generic.DeleteView
class SlotDeleteView(LoginRequiredMixin, PermissionRequiredMixin, SuccessMessageMixin, generic.DeleteView):
# Now, specify the name of the model for which you are creating this DeleteView
model = Slot
# After that, provide the name of the template that will be used to render the content
template_name = "campaign/slot-delete.html"
# And then, set the permissions so that only those user who have certain permissions can access this view
permission_required = ("campaign.delete_slot",)
# At last, provide the success message that we have to show once the delete operation is completed.
success_message = "Slot Deleted Successfully"
# Now, provide the success_url for this view so that we can redirect the user to slot-list page once the delete operation is completed.
def get_success_url(self):
return reverse_lazy("campaign:slot-list", kwargs={"campaign_id": self.get_object().campaign.id})
Templates
Now, create the slot-delete.html file in the templates folder under campaign.
I have attached slot-delete.html file in the resources section. Please download it and paste its content in this html file.
{% extends 'mysite/base.html' %}
{% block title %}
<title>Delete Slot</title>
{% endblock title %}
{% block content %}
<div class="card p-3">
<div class="card-body">
<h4>Are you sure you want to delete "{{ object }}" ?</h4>
{% include "components/form.html" with form_name="Delete Slot" %}
</div>
</div>
{% endblock content %}
URLS
Now, create a url path to access this view.
path("slot/delete/<int:pk>/", views.SlotDeleteView.as_view(), name="slot-delete"),
After that, add this url path in the slot-detail.html
<a href="{% url 'campaign:slot-delete' object.id %}"><i class="fas fa-trash"></i> Delete Slot</a>
[Run the development server and explain the changes]
[Commit the changes and push it in the remote repository.]