Update campaign
In this video, we will create update campaign view that will allow us to update the information of our vaccination campaign.
Resources
Views
In the views.py file, we will create a CampaignUpdateView.
# This view will also inheirt LoginRequiredMixin, PermissionRequiredMixin, SuccessMessageMixin, generic.UpdateView
class CampaignUpdateView(LoginRequiredMixin, PermissionRequiredMixin, SuccessMessageMixin, generic.UpdateView):
# Now, specify the name of the model for which we are performing the update operation
model = Campaign
# Now, specify the form_class
form_class = CampaignForm
# Then, set the permissions for this view.
permission_required = ("campaign.change_campaign",)
# Now, give the name of the template
template_name = "campaign/campaign-update.html"
# After that, specify the success_url
success_url = reverse_lazy("campaign:campaign-list")
# Then, at last, write the success message.
success_message = "Campaign Updated Successfully"
Templates
Now, create a template file named "campaign-update.html" in the templates folder.
I have attached "campaign-update.html" file in the resources section. Please download it and paste its content in this html file.
{% extends 'mysite/base.html' %}
{% block title %}
<title>Update Campaign</title>
{% endblock title %}
{% block content %}
<div class="card p-3">
<div class="card-body">
<h4 class="mb-3">Fill the following details to update campaign details</h4>
{% include "components/form.html" with form_name="Update Campaign" %}
</div>
</div>
{% endblock content %}
URLS
At last, we need to create a url path to access this update view.
path("update/<int:pk>/", views.CampaignUpdateForm.as_view(), name="campaign-update"),
Now, add this url in the campaign-detail.html
<a href="{% url 'campaign:campaign-update' object.id %}"><i class="fas fa-pen"></i> Update Campaign</a>
[Run the development server and see the changes.]