hi everyone, how can i redirect a user to his desire page after login.
I have some pages with a login required re-director. now if the user is not logged in and is prompted to log in, if he is authenticated i want to redirect to the page he wanted to access.
Hello,
Not sure what you are upto, but a quick guide to user authentication and permissions is available at
[Django Tutorial Part 8: User authentication and permissions - Learn web development | MDN](https://Use authenticationand permissions)
ok, here it is. assuming a user is trying to view a particular productnor page but he must be logged in. when he successfully logs in, i want him be redirected to that same product or page he intended to view
I am pretty sure you can put it in the request object.
In the class based views there is a succes_url parameter
If you have enabled the Django user authentication, the @login_required
decorator on your views will do your requirement.
Are you able to route to login and logout pages through urls?
yes i have done that and its working. when a user is prompted to login. he logs in and is redirected to the profile page of the dashboard. but i want it to be able to redirect to that specific page the user wanted to visit before being asked to login
Add @login_required decorator to the view that loads the dashboard page. If a user without authentication tries to access dashboard page, he will be redirected to login page and on successfull login redirected back to dashboard page.
from django.contrib.auth.decorators import login_required
@login_required
def my_view(request):
...
seems like you dont understand the question. i have done that it works but i just want to take it further by redirecting the user to specific pages.
for example: a user wants to post a comment he clicks on the post comment button (which will redirect him to the comment form page) he is prompted to login , after he has successfully logged in, instead of being redirected to the the dashboard by default i want him to be redirected to the comment form page which he initially wanted to visit
you can search for this methodolgy.
<a href="{% url django.contrib.auth.views.login %}?next={{request.path}}">Login</a>
after login it will go to next page which initiated the request.
You need to tweak both your FORM submission as well the login_view
In login_view:
def login_user(request):
next = ""
if request.GET:
next = request.GET['next']
if request.POST:
username = request.POST['username']
password = request.POST['password']
user = authenticate...
login(request, user)
if next == "":
return HttpResponseRedirect(<your default page>)
else:
return HttpResponseRedirect(next)
and your form for login:
{% if next %}
<form action="/login/?next={{next}}" method="post" >
{%else%}
<form action="/login/" method="post" >
{% endif %}
{% csrf_token %}
username:
password:
...
</form>
The @login_required decorator on the view for your comment form page (or the LoginRequiredMixin for CBVs) should handle that automatically. If you’re using standard views for login, you shouldn’t need to change anything else.
i have been unwell for a while. thank you for the reply this did it
It doesn’t work for me
@Cellou404 If you are having an issue similar to this, please open up a new topic with the appropriate details of your code, template, and settings. This topic is marked as “Solved” and is not likely to attract attention.
@kalistos1
I knew your issue has been fixed.
But let me recommend using django-allauth package give a try your life will be easier.
good luck