Adding message to Storage Views
In the last two videos, we learnt how to use message framework in function based view and class based view. In this video, we are going to learn how to use message framework in generics views.
Import
At first, open your views.py file of center. Since we are dealing with generic views, we need a mixin calles SuccessMessageMixin.
from django.contrib.messages.views import SuccessMessageMixin
We will utilize this mixin in the generic views in order to add support for message framework.
Create Storage View
In the create storage view, you have to inherit the SuccessMessageMixin.
class CreateStorage(SuccessMessageMixin, generic.CreateView):
You need to make sure that SuccessMessageMixin comes before generic.CreateView
Now, to add success mesage to this view, you need to add "success_message" in the class.
success_message = "Storage Created Successfully"
In this way, we can add a success message in the generic CreateView.
[Run the development server and see the changes.]
Storage Update View
Similary, in the storage update view, at first, we will inherit SuccessMessageMixin.
class StorageUpdate(SuccessMessageMixin, generic.UpdateView):
Now, we have to add success_message in this class.
success_message = "Storage Updated Successfully"
In this way, we can add a success message in the generic UpdateView.
Storage Delete View
At last, in the storage delete view, we will inherit SuccessMessageMixin.
class StorageDelete(SuccessMessageMixin, generic.DeleteView):
Now, we will add success_message in this class.
success_message = "Storage Deleted Successfully"
[Commit the changes and push it in the remote repository.]