Search code examples
pythondjangodjango-viewsdjango-urlsdjango-settings

Django - after login, redirect user to his custom page --> mysite.com/username


By default after login django redirects the user to an accounts/profile page or if you edit the LOGIN_REDIRECT_URL you can send the user to another page you specify in the settings.py.

This is great but I would like the user (after login) to be redirected to a custom page where the link to that page would look something like this: mysite.com/username. So, the default accounts/profile or the LOGIN_REDIRECT_URL settings would not work in this case since both are somehow static. In my case the username section of the address changes for every user.

Any ideas how I can make it so when the user is logged in would go to a custom user page that has user's name in the address like: mysite.com/username?


Solution

  • You can authenticate and log the user in as stated here: https://docs.djangoproject.com/en/dev/topics/auth/default/#how-to-log-a-user-in

    This will give you access to the User object from which you can get the username and then do a HttpResponseRedirect to the custom URL.