Django mixins用于基于类的通用视图
我正在尝试实现staff_member_required mixins:
I am trying to implement staff_member_required mixins:
这是我找到方法的两种方法:
Here are the two ways I found on how to do so:
第一:
class StaffRequiredMixin(object):
@method_decorator(login_required)
def dispatch(self, request, *args, **kwargs):
if not request.user.is_staff:
messages.error(
request,
'You do not have the permission required to perform the '
'requested operation.')
return redirect(settings.LOGIN_URL)
return super(StaffRequiredMixin, self).dispatch(request,
*args, **kwargs)
第二:
class StaffRequiredMixin(object):
@classmethod
def as_view(self, *args, **kwargs):
view = super(StaffRequiredMixin, self).as_view(*args, **kwargs)
return staff_member_required(view)
@method_decorator(login_required)
def dispatch(self, request, *args, **kwargs):
if not request.user.is_staff:
messages.error(
request,
'You do not have the permission required to perform the '
'requested operation.')
return redirect(settings.LOGIN_URL)
return super(StaffRequiredMixin, self).dispatch(request,
*args, **kwargs)
我想知道的是:
-
为什么第二种方法是覆盖
as_view()
方法并用staff_member_required
包装它?
这样做是否还能获得任何额外"优势?
Do we get any 'additional' advantages by doing so ?
我对这些mixin不熟悉.请帮忙.
I am new to these mixins. Please help.
TL; DR :它们接近相同,区别在于检查is_active
,is_staff
和错误messages
.您可能不需要两者,因为无论如何as_view
替代都会消除对dispatch
替代的需求.
TL; DR: they're close to the same, the difference is in checking is_active
as well as is_staff
and the error messages
. You probably don't need both because the as_view
override negates the need for the dispatch
override anyway.
实际上,这只是对同一件事进行 close 的两种方式.
These are really just two ways of doing close to the same thing.
此代码:
class StaffRequiredMixin(object):
@classmethod
def as_view(self, *args, **kwargs):
view = super(StaffRequiredMixin, self).as_view(*args, **kwargs)
return staff_member_required(view)
...could actually be used alone to implement the staff_member_required
decorator. In this case the staff_member_required
functionality gets called in the view's as_view()
function (i.e., from as_view()
in your URLConf).
此代码:
class StaffRequiredMixin(object):
@method_decorator(login_required)
def dispatch(self, request, *args, **kwargs):
if not request.user.is_staff:
messages.error(
request,
'You do not have the permission required to perform the '
'requested operation.')
return redirect(settings.LOGIN_URL)
return super(StaffRequiredMixin, self).dispatch(request,
*args, **kwargs)
...使用dispatch
方法过滤用户.您可以在Django代码库中看到 as_view
实际调用dispatch
.这意味着如果同时使用 和,则实际上不会触发dispatch
方法中的if not request.user.is_staff
代码,因为任何未通过的用户都会在
...filters users in the dispatch
method. You can see in the Django codebase that as_view
actually calls dispatch
. This means that if you use both together you won't actually ever trigger the if not request.user.is_staff
code in the dispatch
method because any user who doesn't pass would have been filtered out in the as_view
method.
第二个区别是staff_member_required
与第一个代码稍有不同.如果您签出代码,您会注意到staff_member_required
还会检查用户的is_active
标志是否通过(不仅仅是像dispatch
装饰器中的is_staff
).它也不会像代码中那样传递messages.error
.
The second difference is that staff_member_required
is slightly different from what the first code does. If you check out the code, you'll notice that staff_member_required
also checks whether the user's is_active
flag passes (not just is_staff
like in your dispatch
decorator). It also doesn't pass the messages.error
like in your code.