from django.views.generic import DetailView, ListView
from django.views.generic.edit import CreateView, UpdateView, DeleteView
from django.urls import reverse_lazy
from django.shortcuts import render
from django.contrib.messages.views import SuccessMessageMixin
from product.models import ProductCategory

# Create your views here.

class ProductCategoryList(ListView):
    model = ProductCategory
    context_object_name = 'product_categories'

class ProductCategoryDetail(DetailView):
    model = ProductCategory
    context_object_name = 'product_category'

class ProductCategoryCreate(CreateView, SuccessMessageMixin):
    model = ProductCategory
    fields = ['name']
    success_message = "Category %(name)s created"

class ProductCategoryUpdate(UpdateView, SuccessMessageMixin):
    model = ProductCategory
    fields = ['name']
    success_message = "Category %(name)s updated"

class ProductCategoryDelete(DeleteView, SuccessMessageMixin):
    model = ProductCategory
    context_object_name = 'product_category'
    success_url = reverse_lazy('product-category-list')
    success_message = "Category %(name)s removed"

from django.http import HttpResponseRedirect
from django.views.generic import DetailView, ListView
from django.views.generic.edit import CreateView, UpdateView, DeleteView
from django.urls import reverse_lazy
from django.shortcuts import render
from django.contrib import messages
from django.contrib.messages.views import SuccessMessageMixin
from product.models import ProductCategory

# Create your views here.

class ProductCategoryList(ListView):
    model = ProductCategory
    context_object_name = 'product_categories'

class ProductCategoryDetail(DetailView):
    model = ProductCategory
    context_object_name = 'product_category'

class ProductCategoryCreate(SuccessMessageMixin, CreateView):
    model = ProductCategory
    fields = ['name']
    success_message = "Category %(name)s created"

class ProductCategoryUpdate(SuccessMessageMixin, UpdateView):
    model = ProductCategory
    fields = ['name']
    success_message = "Category %(name)s updated"

class ProductCategoryDelete(DeleteView):
    model = ProductCategory
    context_object_name = 'product_category'
    success_url = reverse_lazy('product-category-list')
    success_message = "Category removed"
    cancel_message = "Removal cancelled"

    def post(self, request, *args, **kwargs):
        if "cancel" in request.POST:
            url = self.success_url
            messages.warning(self.request, self.cancel_message)
            return HttpResponseRedirect(url)
        else:
            messages.success(self.request, self.success_message)
            return super(ProductCategoryDelete, self).delete(request, 
                    *args, **kwargs)


Make messages show in delete view.
Make messages show in delete view.

+ from django.http import HttpResponseRedirect
  from django.views.generic import DetailView, ListView
  from django.views.generic.edit import CreateView, UpdateView, DeleteView
  from django.urls import reverse_lazy
  from django.shortcuts import render
+ from django.contrib import messages
  from django.contrib.messages.views import SuccessMessageMixin
  from product.models import ProductCategory
  
  # Create your views here.
  
  class ProductCategoryList(ListView):
      model = ProductCategory
      context_object_name = 'product_categories'
  
  class ProductCategoryDetail(DetailView):
      model = ProductCategory
      context_object_name = 'product_category'
  
- class ProductCategoryCreate(CreateView, SuccessMessageMixin):
+ class ProductCategoryCreate(SuccessMessageMixin, CreateView):
      model = ProductCategory
      fields = ['name']
      success_message = "Category %(name)s created"
  
- class ProductCategoryUpdate(UpdateView, SuccessMessageMixin):
+ class ProductCategoryUpdate(SuccessMessageMixin, UpdateView):
      model = ProductCategory
      fields = ['name']
      success_message = "Category %(name)s updated"
  
- class ProductCategoryDelete(DeleteView, SuccessMessageMixin):
+ class ProductCategoryDelete(DeleteView):
      model = ProductCategory
      context_object_name = 'product_category'
      success_url = reverse_lazy('product-category-list')
-     success_message = "Category %(name)s removed"
+     success_message = "Category removed"
+     cancel_message = "Removal cancelled"
  
+     def post(self, request, *args, **kwargs):
+         if "cancel" in request.POST:
+             url = self.success_url
+             messages.warning(self.request, self.cancel_message)
+             return HttpResponseRedirect(url)
+         else:
+             messages.success(self.request, self.success_message)
+             return super(ProductCategoryDelete, self).delete(request, 
+                     *args, **kwargs)
+ 
+ 
Make messages show in delete view.
## Code Before:
from django.views.generic import DetailView, ListView
from django.views.generic.edit import CreateView, UpdateView, DeleteView
from django.urls import reverse_lazy
from django.shortcuts import render
from django.contrib.messages.views import SuccessMessageMixin
from product.models import ProductCategory

# Create your views here.

class ProductCategoryList(ListView):
    model = ProductCategory
    context_object_name = 'product_categories'

class ProductCategoryDetail(DetailView):
    model = ProductCategory
    context_object_name = 'product_category'

class ProductCategoryCreate(CreateView, SuccessMessageMixin):
    model = ProductCategory
    fields = ['name']
    success_message = "Category %(name)s created"

class ProductCategoryUpdate(UpdateView, SuccessMessageMixin):
    model = ProductCategory
    fields = ['name']
    success_message = "Category %(name)s updated"

class ProductCategoryDelete(DeleteView, SuccessMessageMixin):
    model = ProductCategory
    context_object_name = 'product_category'
    success_url = reverse_lazy('product-category-list')
    success_message = "Category %(name)s removed"

## Instruction:
Make messages show in delete view.
## Code After:
from django.http import HttpResponseRedirect
from django.views.generic import DetailView, ListView
from django.views.generic.edit import CreateView, UpdateView, DeleteView
from django.urls import reverse_lazy
from django.shortcuts import render
from django.contrib import messages
from django.contrib.messages.views import SuccessMessageMixin
from product.models import ProductCategory

# Create your views here.

class ProductCategoryList(ListView):
    model = ProductCategory
    context_object_name = 'product_categories'

class ProductCategoryDetail(DetailView):
    model = ProductCategory
    context_object_name = 'product_category'

class ProductCategoryCreate(SuccessMessageMixin, CreateView):
    model = ProductCategory
    fields = ['name']
    success_message = "Category %(name)s created"

class ProductCategoryUpdate(SuccessMessageMixin, UpdateView):
    model = ProductCategory
    fields = ['name']
    success_message = "Category %(name)s updated"

class ProductCategoryDelete(DeleteView):
    model = ProductCategory
    context_object_name = 'product_category'
    success_url = reverse_lazy('product-category-list')
    success_message = "Category removed"
    cancel_message = "Removal cancelled"

    def post(self, request, *args, **kwargs):
        if "cancel" in request.POST:
            url = self.success_url
            messages.warning(self.request, self.cancel_message)
            return HttpResponseRedirect(url)
        else:
            messages.success(self.request, self.success_message)
            return super(ProductCategoryDelete, self).delete(request, 
                    *args, **kwargs)

