基于类的视图转换函数与Django中的装饰器

时间:2016-07-07 14:20:12

标签: django login decorator mixins

我有这段代码:

PersonList(LoginRequiredMixin, ListView)
    model = Person
    paginate_by = 10

如何在ListView中修改此函数以适应装饰器?

我试试:

var htmltext='test message <a class="red pagtag" href="#" contenteditable="false">Harshvardhan Rane</a> test <br> <a class="red pagtag" href="#" contenteditable="false">Jay Alvarrez</a>';
var $html = $("<div/>").html(htmltext);
$html.find("a").each(function() {
  $(this).replaceWith("**"+$(this).text()+"**");
});
$("body").append($html)

我不知道如何实现上面提到的装饰器。

1 个答案:

答案 0 :(得分:0)

Engraçado,sófuncionoudesse jeito。

from django.utils.decorators import method_decorator


class SuperuserRequiredMixin(object):

    @method_decorator(user_passes_test(lambda u: u.is_authenticated() and u.is_superuser))
    def dispatch(self, *args, **kwargs):
        return super(SuperuserRequiredMixin, self).dispatch(*args, **kwargs)


class PersonList(SuperuserRequiredMixin, ListView):
    model = Person
    paginate_by = 20