简体   繁体   中英

user_passes_test decorator redirecting to login even when result is True

This is my decorator:

def is_clinic(user):
    user.groups.filter(name='Klinik').exists()

And my view:

class Index(View):
    @method_decorator(login_required)
    @method_decorator(user_passes_test(is_clinic, login_url='/clinic_only.html'))
    def get(self, request):
        return render(request,"index.html")

user is in group. I tested it with shell and decorator is returning True for sure.

When i navigate to url, django is redirecting me to clinic_only.html

You have forgotten to return the result of your query. Therefore, your function implicitly returns None , and the decorator redirects to the login page. You should have:

def is_clinic(user):
    return user.groups.filter(name='Klinik').exists()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM