djangoauthenticationdecorator

Use staff_member_required decorator, but without being redirected to the admin interface?


When I try to use staff_view, I get redirected in the admin authentication interface.

    from django.contrib.admin.views.decorators import staff_member_required

    @staff_member_required
    def staff_view(request..):
        ...

How can I make a custom login, and not getting redirected in the default admin login interface?


Solution

  • You can use Django's user_passes_test decorator:

    from django.contrib.auth.decorators import user_passes_test
    
    @user_passes_test(lambda u:u.is_staff, login_url=reverse_lazy('foo'))
    def staff_view(request..):
        ...