【发布时间】:2026-01-31 05:10:01
【问题描述】:
我正在 django 中开发一个简单的“待办事项”应用程序(两种模型:List 和 Item)。尝试学习和使用基于类的通用视图。我有以下三个显示视图工作,但我请求快速代码审查,看看在我继续创建、更新和删除之前,我是否可以做些什么来改进我对 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
【问题讨论】:
-
我特别关心在dispatch()中添加业务逻辑。它有效,但我怀疑有更好/更“标准”的方式?
-
这个问题似乎是题外话,因为它是关于审查代码的,应该在codereview.stackexchange.com
-
糟糕,谢谢马克西姆。我不知道 codereview.stackexchange.com。我去那里试试。
标签: python django django-class-based-views django-generic-views