Login redirect Django does not work

Asked

Viewed 169 times

1

I am trying to make a redirect of users based on functions, example: an admin after the login is sent to the admin, the attendant or common employee, be sent to a home, but the code placed does not redirect, just back to home, for all functions, how to solve?

VIEWS

    @login_required
def login(request):
    if request.method == 'POST':
        username = request.POST.get('username')
        password = request.POST.get('password')
        user = authenticate(username=username, password=password)

        if user.is_active:    
        # Redirecting to the required login according to user status.
            if user.is_superuser or user.is_staff:
                login(request, user)
                return HttpResponseRedirect('admin')  # or your url name
            else:
                login(request, user)
                return redirect('')

SETTING.PY

    LOGIN_URL = 'login'
LOGIN_REDIRECT_URL = '/'
LOGOUT_REDIRECT_URL = 'home'

2 answers

0

Look, I don’t know if that’s all it is, because in my head, I would structure it differently. I identified that on your line 6 you are giving the function of authenticating the user. But on line 1 you request that the user is authenticated. We have a deadlock problem here... can fix that and see if it was the only problem?

0

You need to pass the url path as a parameter or the name of a view to be redirected as in the documentation example. Which name you set for view login in urls.py?

1.Passing the view name

def my_view(request):
...
return redirect('some-view-name', foo='bar')

2.Passing the url properly

def my_view(request):
...
return redirect('/some/url/')


def my_view(request):
...
return redirect('https://example.com/')
  • 1

    Oh my dear, thank you very much, that’s right, thank you.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.