Python 基于简单django类的泛型视图:我做得对吗?

Python 基于简单django类的泛型视图:我做得对吗?,python,django,django-class-based-views,django-generic-views,Python,Django,Django Class Based Views,Django Generic Views,我正在django中开发一个简单的“待办事项列表”应用程序(两种模式:列表和项目)。尝试学习并使用基于类的泛型视图。我有以下三个显示视图可以使用,但我要求快速查看代码,看看在我继续创建、更新和删除之前,我是否可以改进对django的ListView和DetailView的使用/理解。提前谢谢你的建议 # class-based generic views for my lists, a list's items, and item detail... class MyListsView(List

我正在django中开发一个简单的“待办事项列表”应用程序(两种模式:列表和项目)。尝试学习并使用基于类的泛型视图。我有以下三个显示视图可以使用,但我要求快速查看代码,看看在我继续创建、更新和删除之前,我是否可以改进对django的ListView和DetailView的使用/理解。提前谢谢你的建议

# class-based generic views for my lists, a list's items, and item detail...
class MyListsView(ListView):
    """Display My Lists"""
    template_name = 'cmv_app/my_lists.html'
    context_object_name = 'my_lists'

    def get_queryset(self):
        # override get_queryset() to filter in only lists belonging to the current user,
        # eliminating the need to define a model in this class.
        return List.objects.filter(user=self.request.user).order_by('-last_modified')

    # override dispatch to decorate this view with login_required
    @method_decorator(login_required)
    def dispatch(self, *args, **kwargs):
        return super(MyListsView, self).dispatch(*args, **kwargs)


class ListItemsView(ListView):
    """Display a list's items"""
    template_name = 'cmv_app/list_items.html'
    context_object_name = 'list_items'

    def get_queryset(self):
        # get a particular list's items
        items = Item.objects.filter(list=self.kwargs['list_id']).order_by('-created_date')
        return items

    def get_context_data(self, **kwargs):
        # Call the base implementation first to get a context
        context = super(ListItemsView, self).get_context_data(**kwargs)
        # Add the list, needed for the template to lookup urls for the item detail page
        context['list'] = self.list
        return context

    @method_decorator(login_required)
    def dispatch(self, request, *args, **kwargs):
        self.list = get_object_or_404(List, pk=kwargs['list_id'])
        # this is a security check: a user can only access his own lists 
        # (else raise PermissionDenied)
        request = list_valid_for_user(request, self.list) 
        return super(ListItemsView, self).dispatch(request, *args, **kwargs)


class ItemDetailView(DetailView):
    """Show detail for one item"""
    template_name = 'cmv_app/item_detail.html'

    def get_object(self):
        return self.item

    @method_decorator(login_required)
    def dispatch(self, request, *args, **kwargs):
        # fetching list and item in here instead of in get_object() in order to implement
        # security checks
        self.list = get_object_or_404(List, pk=kwargs['list_id'])
        self.item = get_object_or_404(Item, pk=kwargs['item_id'])
        # user can only access his lists (else raise PermissionDenied)
        request = list_valid_for_user(request, self.list) 
        # item must be associated with the specified list (else raise PermissionDenied)
        request = item_valid_for_list(request, self.item, self.list)
        return super(ItemDetailView, self).dispatch(request, *args, **kwargs)

    def get_context_data(self, **kwargs):
        # Call the base implementation first to get a context
        context = super(ItemDetailView, self).get_context_data(**kwargs)
        # Add in the elements needed for the template to construct url for link to list
        context['list'] = self.list
        return context

例如,对于
列表项视图
,在您的分派中仅执行以下操作:

self.list = kwargs['list']
self.list_id = kwargs['list_id']
然后,转到
get\u context\u data

self.list = get_object_or_404(List, pk=self.list_id)
这将转到
get\u queryset

items = Item.objects.filter(list=self.list_id).order_by('-created_date')

我特别关注在dispatch()中添加业务逻辑。它可以工作,但我怀疑有更好/更“标准”的方法吗?这个问题似乎离题了,因为它是关于检查代码的,应该放在Oops上,谢谢Maxime。我不知道codereview.stackexchange.com。我在那里试试。