python - 简单的 django 基于类的通用 View : am I doing it right?

标签 python django django-class-based-views django-generic-views

我正在 django 中开发一个简单的“待办事项列表”应用程序(两个模型:列表和项目)。尝试学习和利用基于类的通用 View 。我有以下三个显示 View 正在运行,但我要求快速进行代码审查,看看在继续创建、更新和删除之前,我是否可以做些什么来提高对 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

最佳答案

例如,对于 ListItemsViews,在您的调度中只需执行以下操作:

self.list = kwargs['list']
self.list_id = kwargs['list_id']

然后,转到get_context_data:

self.list = get_object_or_404(List, pk=self.list_id)

这将转到get_queryset:

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

关于python - 简单的 django 基于类的通用 View : am I doing it right?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23877494/

相关文章:

python - 如何从其他以字典为元素的列表中获取python列表

python - azcopy:由于错误而无法解析用户输入:当前不支持推断的源/目标组合

django - 编写一个同时显示基于日期的对象和表单的基于 django 类的 View 的好方法是什么?

python - Django:正确使用基于类的 View 继承?

python - 在 Python 中使用多处理创建超时函数

Python 3.6 嵌套字典动态更新

django - 您可以获取 DRF ModelViewSet 中的请求方法吗?

django - 用 celery 运行管理命令?

django 迁移无需迁移

django - 使用基于类的 View 将模板的一部分限制为属于组的用户的访问权限。 Django 2.0