خرید بک لینک

Vote count: 0

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:

  1. Why the second way is overriding the as_view() method and wrapping it with staff_member_required ?

  2. Do we get any 'additional' advantages by doing so ?

I am new to these mixins. Please help.

asked 4 mins ago

برچسب: نویسنده: استخدام کار تاريخ: جمعه 27 فروردين 1395 ساعت: 12:39

صفحه بندی