I am trying to implement staff_member_required mixins:
Here are the two ways I found on how to do so:
First:
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.')
retu redirect(settings.LOGIN_URL)
retu super(StaffRequiredMixin, self).dispatch(request,
*args, **kwargs)
Second:
class StaffRequiredMixin(object):
@classmethod
def as_view(self, *args, **kwargs):
view = super(StaffRequiredMixin, self).as_view(*args, **kwargs)
retu 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.')
retu redirect(settings.LOGIN_URL)
retu super(StaffRequiredMixin, self).dispatch(request,
*args, **kwargs)
What I want to know is:
-
Why the second way is overriding the
as_view()method and wrapping it withstaff_member_required? -
Do we get any 'additional' advantages by doing so ?
I am new to these mixins. Please help.
