小编Mil*_*vic的帖子

Use LoginRequiredMixin and UserPassesTestMixin at the same time

I want to have a TemplateView Class that uses LoginRequiredMixin and UserPassesTestMixin at the same time. Something like this:

from django.views.generic import TemplateView
from django.contrib.auth.mixins import LoginRequiredMixin, UserPassesTestMixin

class FinanceOverview(LoginRequiredMixin, UserPassesTestMixin, TemplateMixin):
    login_url = '/login'
    redirect_field_name = 'next'

    def test_func(self):
        return self.request.user.groups.filter(name="FinanceGrp").exists()

    def get(self, request, *args, **kwargs):
        DO SOMETHING IF USER IS AUTHENTICATED AND ALSO MEMBER OF GROUP FinanceGrp
Run Code Online (Sandbox Code Playgroud)

Basically as you can see above, what I want to achieve is the following:

  • If user is not authenticated, to redirect …

django mixins django-views python-3.x

5
推荐指数
1
解决办法
1665
查看次数

标签 统计

django ×1

django-views ×1

mixins ×1

python-3.x ×1