成功消息不适用于创建视图

时间:2019-02-08 09:16:03

标签: django

我使用过SuccessMessageMixin类,但是在createview中也没有收到成功消息,但是在updateview中,当我返回super().form_valid(form)

时,它正在工作
class DepartmentCreateView(LoginRequiredMixin, PermissionRequiredMixin,SuccessMessageMixin ,CreateView):

template_name = 'departments/create_department.html'
form_class = DepartmentForm
success_url = reverse_lazy('departments:departments')
permission_required = ('departments.add_department',)

    def form_valid(self, form):

       department = form.save(commit=False)
       department.created_by = self.request.user
       department.updated_by = self.request.user
       department.slug = slugify(uuid.uuid4())
       department.save()

       message = '[{"created": {}}]'

       # retriving ContentType object
       ct_obj = ContentType.objects.get(model='department')

       # creating history object
       history = History.objects.create(
           action_time=timezone.now(),
           action='created',
           user=department.created_by,
           content_type=ct_obj,
           object_id=department.id,
           change_message=message,
       )
       history.save()
       return super().form_valid(form)

1 个答案:

答案 0 :(得分:4)

您实际上尚未设置成功消息。

MultipartFile

请注意,您的class DepartmentCreateView(LoginRequiredMixin, PermissionRequiredMixin,SuccessMessageMixin ,CreateView): success_message = "Department was created successfully" ... 保存了两次。您应该这样做:

form_valid