0

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?

Meysam
  • 409
  • 4
  • 14
  • did you checked this post https://stackoverflow.com/a/7061358/14457833 – Ankit Tiwari Feb 05 '22 at 09:32
  • Does this answer your question? [Redirect to Next after login in Django](https://stackoverflow.com/questions/38431166/redirect-to-next-after-login-in-django) – Abdul Aziz Barkat Feb 05 '22 at 09:34
  • @AbdulAzizBarkat In that question it always redirects to certain page `(next)`. It can comes from many pages. – Meysam Feb 05 '22 at 09:42
  • 1
    `next` is not _fixed_ it is a querystring / GET parameter. If you look carefully when you get redirected to the login page due to the `login_required` decorator there should be a `next=` in your url... In fact if you used the builtin [LoginView](https://docs.djangoproject.com/en/4.0/topics/auth/default/#django.contrib.auth.views.LoginView) that is what it does. – Abdul Aziz Barkat Feb 05 '22 at 09:47

0 Answers0