Django 的 success_message 基于 form_valid,因为删除视图不包含表单验证,因此 SuccessMessageMixin 在这里不起作用。

解决办法

from django.shortcuts import reverse
from django.contrib import messages
from django.contrib.messages.views import SuccessMessageMixin
from django.views.generic.edit import DeleteView

from company.models import ProductImage

class ProductDelete(LoginRequiredMixin, SuccessMessageMixin, DeleteView):
    model = ProductImage
    template_name = 'myapp/product_del.html'
    success_message = '产品删除成功!'

    def get_success_url(self):
        return reverse('myapp.product')

    def delete(self, request, *args, **kwargs):
        messages.success(self.request, self.success_message)
        return super(ProductDelete, self).delete(request, *args, **kwargs)

Reference