当我使用基于功能的视图时,我经常这样做:
def my_view(request, username):
user = get_object_or_404(User, username = username)
if user != request.user:
return HttpRequestForbidden()
此代码确保只有用户john
才能访问/users/john/edit/
等网页。
如何使用基于类的视图执行相同的操作?
答案 0 :(得分:2)
我的问题似乎与Example of Django Class-Based DeleteView
重复这是怎么做的:
def get_object(self, queryset=None):
""" Hook to ensure object is owned by request.user. """
obj = super(MyDeleteView, self).get_object()
if not obj.owner == self.request.user:
raise Http404
return obj