'Django @login_required doesn't work when login function has redirect

In login view I have:

def login(request):
    ...
    login(request, user)
    messages.success(request, 'You have successfully logged in to your account.')
    return redirect('user_app:profile')

In some other function I have @login_required(login_url='/login/') but their redirect doesn't work and allways redirect to user_app:profile.

@login_required(login_url='/login/') #redirect to profile page instead of jobs page
def jobs(request):
    ...

@login_required(login_url='/login/') #redirect to profile page instead of offers page
def offers(request):
    ...

How do I redirect a user to page he/she redirected from it to login page, and redirect to the profile page only when he/she referred directly to the login page?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source