Create Campaign
In this video, we are going to create CampaignCreate View that allows us to create vaccination campaign instance.
Resources
Forms
At first, we need to create a form.py file in the Campaign app.
# here at first we will import the ModelForm
from django.forms import ModelForm
# Then, we will import Campaign model because we want to create modelform for this campaign model
from campaign.models import Campaign
# Now Create a CampaignForm and inherit the ModelForm
class CampaignForm(ModelForm):
# I also want to add form-control to every form element, so I need to add form-control.
# [Copy the below code from other place]
def __init__(self, *args, **kwargs):
super(CampaignForm, self).__init__(*args, **kwargs)
for visible in self.visible_fields():
visible.field.widget.attrs["class"] = "form-control"
# Then specify the meta class and provide the name of model for this form and the corresponding fields.
class Meta:
model = Campaign
fields = "__all__"
In this way, we have created a form for Campaign.
Views
Now in the views.py file,
# At First, I will import the CampaignForm which we created just now.
from campaign.forms import CampaignForm
# [Import below files during explanation]
from django.contrib.auth.mixins import LoginRequiredMixin, PermissionRequiredMixin
from django.contrib.messages.views import SuccessMessageMixin
from django.urls import reverse_lazy
# Now create a CampaignCreateView and then inherit LoginRequiredMixin, PermissionRequiredMixin, SuccessMessageMixin, generic.CreateView
class CampaignCreateView(LoginRequiredMixin, PermissionRequiredMixin, SuccessMessageMixin, generic.CreateView):
# Now specify the model name for this generic Create View
model = Campaign
# Then specify the form_class
form_class = CampaignForm
# Now, you have to specify the permissions that are required to access this view
permission_required = ("campaign.add_campaign",)
# Now, provide the template name for this view
template_name = "campaign/campaign-create.html"
# After that, we need to mention the success url. Because once the user completes the create operation, we have to redirect that user to a certain page.
success_url = reverse_lazy("campaign:campaign-list")
# At last, we will mention the success message to show to the user for the successful create operation.
success_message = "Campaign Created Successfully"
Templates
Since, we haven't created the "campaign-create.html" file, so lets create it.
I have attached a file named "campaign-create.html" in the resources section. Please download it and paste its content in this file.
{% extends 'mysite/base.html' %}
{% block title %}
<title>Create New Campaign</title>
{% endblock title %}
{% block content %}
<div class="card p-3">
<div class="card-body">
<h4 class="mb-3">Fill the following details to create a new campaign</h4>
{% include "components/form.html" with form_name="Create Campaign" %}
</div>
</div>
{% endblock content %}
URLS
Now, we need to add url for the create view.
path("create/", views.CampaignCreateView.as_view(), name="campaign-create"),
Now, we have to add this url path in the campaign-list.html file.
<a href="{% url 'campaign:campaign-create' %}"><i class="fas fa-plus"></i> Create Campaign</a>
[Run the development server and see the changes]