Skip to content

Delete Campaign

In this video, we are going to create the delete view to allows us to delete the vaccination campaign from our database.

Resources
- campaign-delete.html

Views

In the views.py file, lets create a CampaignDeleteView.

# This view will inherit LoginRequiredMixin, PermissionRequiredMixin, SuccessMessageMixin, generic.DeleteView
class CampaignDeleteView(LoginRequiredMixin, PermissionRequiredMixin, SuccessMessageMixin, generic.DeleteView):
    # Now, specify the model for which you are creating this deleteview
    model = Campaign
    # Now, give the template name for this view
    template_name = "campaign/campaign-delete.html"
    # After that, set the permissions required to access this view
    permission_required = ("campaign.delete_campaign",)
    # Then, add a success url that will be used while redirecting the user after delete operation is completed.
    success_url = reverse_lazy("campaign:campaign-list")
    # At last, mention the success message.
    success_message = "Campaign Deleted Successfully"

Templates

Now, lets create the campaign-delete.html file in the templates folder.

I have attached campaign-delete.html in the resources section. Please download it and paste its content in this html file.

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

{% block title %} 
    <title>Delete Campaign</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 Campaign" %}
    </div>
</div>
{% endblock content %}

URLS

Now, lets create the url path for this delete view.

    path("delete/<int:pk>/", views.CampaignDeleteView.as_view(), name="campaign-delete"),

After that, we need to add this url path in the campaign-detail.html file.

<a href="{% url 'campaign:campaign-delete' object.id %}"><i class="fas fa-trash"></i> Delete Campaign</a>

[Run the development server and see the changes.]

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