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

3

25

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?

Bergeron answered 2/8, 2012 at 9:28 Comment(1)
You'll have to write your own decorator :)Bleat
M
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..):
    ...
Muscid answered 2/8, 2012 at 9:37 Comment(0)
Y
25

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..):
    ...
Yangyangtze answered 4/2, 2017 at 9:44 Comment(0)
O
0

@staff_member_required(login_url="/warning/staff/member") def staff_only_view(request): return render(request,"protected/staff_only.html")

add path where you want to redirect login_url="/warning/staff/member" and it will redirect

Opacity answered 1/8 at 10:38 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.