[Django]-Use staff_member_required decorator, but without being redirected to the admin interface?

28👍

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..):
    ...

24👍

It’s worth pointing out that staff_member_required is a wrapper around user_passes_test (see source code) and it also accepts a login_url parameter, so you could do:

@staff_member_required(login_url=example_url)
def staff_view(request..):
    ...

Leave a comment