How can I not use Django's admin login view?

后端 未结 10 2668
借酒劲吻你
借酒劲吻你 2020-12-28 15:11

I created my own view for login. However if a user goes directly to /admin it brings them to the admin login page and doesn\'t use my custom view. How can I make it redirect

10条回答
  •  刺人心
    刺人心 (楼主)
    2020-12-28 15:30

    I had the same issue, tried to use the accepted answer, but has the same issue as pointed in the comment above. Then I've did something bit different, pasting here if this would be helpful to someone.

    def staff_or_404(u):
        if u.is_active:
            if u.is_staff:
                return True
            raise Http404()
        return False
    
    admin.site.login = user_passes_test(
            staff_or_404,
        )(admin.site.login)
    

    The idea is that if the user is login, and tried to access the admin, then he gets 404. Otherwise, it will force you to the normal login page (unless you are already logged in)

提交回复
热议问题