Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 ? Any input is truly appreciated.

like image 292
avatar Avatar asked Feb 02 '11 03:02

avatar


People also ask

How do I redirect one page to another in Django?

Django Redirects: A Super Simple Example Just call redirect() with a URL in your view. It will return a HttpResponseRedirect class, which you then return from your view. Assuming this is the main urls.py of your Django project, the URL /redirect/ now redirects to /redirect-success/ .

Which of the following will redirect a user to profile mine after successful login?

auth. views. login redirects you to accounts/profile/ right after you log in.

How do I add login requirements in Django?

Create an app folder in the django project folder. Add template folder in the django folder and provide its path in django_folder > settings.py . Create file named as urls.py in the app folder and provide its path in django_project > urls.py. Add login decorator to the function in app_folder > views.py.

What is HTTP Response redirect in Django?

HttpResponseRedirect is a subclass of HttpResponse (source code) in the Django web framework that returns the HTTP 302 status code, indicating the URL resource was found but temporarily moved to a different URL. This class is most frequently used as a return object from a Django view.


2 Answers

A simpler approach relies on redirection from the page LOGIN_REDIRECT_URL. The key thing to realize is that the user information is automatically included in the request.

Suppose:

LOGIN_REDIRECT_URL = '/profiles/home' 

and you have configured a urlpattern:

(r'^profiles/home', home), 

Then, all you need to write for the view home() is:

from django.http import HttpResponseRedirect from django.urls import reverse from django.contrib.auth.decorators import login_required  @login_required def home(request):     return HttpResponseRedirect(                reverse(NAME_OF_PROFILE_VIEW,                         args=[request.user.username])) 

where NAME_OF_PROFILE_VIEW is the name of the callback that you are using. With django-profiles, NAME_OF_PROFILE_VIEW can be 'profiles_profile_detail'.

like image 96
Stu Avatar answered Sep 16 '22 16:09

Stu


Yes! In your settings.py define the following

LOGIN_REDIRECT_URL = '/your-path' 

And have '/your-path' be a simple View that looks up self.request.user and does whatever logic it needs to return a HttpResponseRedirect object.

A better way might be to define a simple URL like '/simple' that does the lookup logic there. The URL looks more beautiful, saves you some work, etc.

like image 32
Jarvis Jones Avatar answered Sep 18 '22 16:09

Jarvis Jones



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!