Example of Django Class-Based DeleteView

后端 未结 4 1276
长情又很酷
长情又很酷 2020-11-30 18:41

Does anyone know of or can anyone please produce a simple example of Django\'s class-based generic DeleteView? I want to subclass DeleteView and ensure that the currently lo

4条回答
  •  栀梦
    栀梦 (楼主)
    2020-11-30 18:50

    I would propose that the best (and simplest) way to do this would be to use the UserPassesTestMixin which gives you a cleaner separation of concerns.

    Example:

    from django.contrib.auth.mixins import LoginRequiredMixin, UserPassesTestMixin
    from django.views.generic import DeleteView
    
    
    class MyDeleteView(LoginRequiredMixin, UserPassesTestMixin, DeleteView):
        def test_func(self):
            """ Only let the user access this page if they own the object being deleted"""
            return self.get_object().owner == self.request.user
    

提交回复
热议问题