pythondjango-viewsdjango-urls

How to override the CB LoginView in Django


I have this url for my login. I'd like to have this login redirect to different pages depending the level of the user i.e admin or superuser.

How do I do it????

url(r'^login/$',  auth_views.LoginView.as_view(), name='login'),

Solution

  • You just need to overwrite get_success_url method on LoginView, create custom login.html file and swap default LoginView for your custom one

    from django.contrib.auth.views import LoginView
    
    
    class CustomLoginRedirectView(LoginView):
        def get_success_url(self):
            if not self.request.user.is_staff:
                return 'https://www.google.com/'
            else:
                return 'https://stackoverflow.com/questions/70925589/how-to-override-the-cb-loginview-in-django'