Skip to content

Adding message to Vaccine Views

In the last, video, we learnt how to use message framework in function based view. In this video, we will learnt how to use message framework in class based view.

Create Vaccine View

Open your views.py file of vaccine. At first, import the messages from django.contrib

from django.contrib import messages

Now, in the create vaccine view, when the form data is valid, we will show success message and when the form data is invalid, we will show an error message.

messages.success(request, "Vaccine Added Successfully")

So, when the form is valid and saved, user will see "Vaccine Added Successfully" message in the vaccine list page.

Similary, when the form data is invalid, we will show an error message.

messages.error(request, "Please enter valid data")

[Run the development server and see the changes.]

Update Vaccine View

Similary, we will also add message framework in the update vaccine view. When the form data is valid, we will show success message and when the form data is invalid, we will show an error message.

messages.success(request, "Vaccine Updated Successfully")

So, when the form is valid and saved, user will see "Vaccine Updated Successfully" message in the vaccine detail page.

Similary, when the form data is invalid, we will show an error message.

messages.error(request, "Please enter valid data")

[Run the development server and see the changes.]

Delete Vaccine

At last, we will add messages in the delete vaccine view.

When the vaccine object is deleted, we will show message sucess.

messages.success(request, "Vaccine Deleted Successfully")

[Run the development server and see the changes.]

In this way, we have learnt how to use message framework in the class based views.