Django mixins用于基于类的通用视图 [英] Django mixins for class-based-generic views

查看:334
本文介绍了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)

我想知道的是:

  1. 为什么第二种方法是覆盖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_activeis_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.

这篇关于Django mixins用于基于类的通用视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆