django user_passes_test装饰器 [英] django user_passes_test decorator

查看:593
本文介绍了django user_passes_test装饰器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何为基于类的视图实施 @user_passes_test(lambda u:u.is_superuser)装饰器?我以前使用过这个功能的视图,我有一个解决方法,但感觉不自然。

How do I implement the @user_passes_test(lambda u: u.is_superuser) decorator for class based views? I have used this before for function based views, and I have a workaround but it feels unnaturally.

这不应该被调度方法覆盖?

Shouldn't this be covered by the dispatch method?

推荐答案

您在 dispatch上使用 @method_decorator c $ c>类的方法:

You use @method_decorator on the dispatch method of the class:

from django.views.generic import View
from django.utils.decorators import method_decorator
from django.contrib.auth.decorators import user_passes_test

class MyView(View):
    @method_decorator(user_passes_test(lambda u: u.is_superuser))
    def dispatch(self, *args, **kwargs):
        return super(MyView, self).dispatch(*args, **kwargs)

这篇关于django user_passes_test装饰器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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