Django - Login and redirect to user profile page

Viewed 7079

I am trying to redirect a user who just logged in to his/her's respective account page.

This question has been asked a few times, but most of them are old and use static urls like /accounts/profile/: Django - after login, redirect user to his custom page --> mysite.com/username. I would like to use dynamic url naming to achieve this solution.

For example, what if my account landing page has the following url pattern?

url(r'^account/(?P<pk>\d+)/(?P<name>\w+)/$', AccountLanding.as_view(), name="account-landing" )`.

How would I pass the args in settings.py for LOGIN_REDIRECT_URL?

2 Answers

On Django 4.x there is a new attribute called next_page and you can use it as:

from django.contrib.auth.views import LoginView
from django.urls import reverse_lazy

class MyLoginView():
...
next_page = reverse_lazy('profile', kwargs={'id': pk},
                            name='username'))
...
Related