Django redirection after successful login - django

Is it possible that after successful login first redirect to a new page and not to that url witch is in the next parameter (ex. http://localhost:8000/ro/login/?next=/ro/exam/3/). So what I exactly want is after a successful login first redirect to that url what I provided in LOGIN_REDIRECT_URL, and also I want to keep somehow the next url ( http://localhost:8000/ro/login/?next=/ro/exam/3/), because after the user clicks somewhere I want to redirect him to the target.

Ciao, why don't you redirect the user in LOGIN_REDIRECT_URL on login button click, and then on another user action send him to ( http://localhost:8000/ro/login/?next=/ro/exam/3/) (stored somewhere as a string) ?

Related

Facebook Graph API add extra parameter to login url

is there any way to get back my users to the current page after they login and redirect to my callback url? for example my current page url is:
https://mydomain.test/post1
then my callback url is:
https:/mydomain.test/callback.php
I tried to add extra parameter to login url but got an error because of the Valid OAuth Redirect URIs url match. my url is dynamic so I need a way to set parameters that changes and start from there.

Accessing URL in 'get' method of view

I want a web page (with the url page3) to be displayed differently depending on whether a user on my website is redirected to it from the pages with urls page1 or page2.
How can I access the full url (not just the query parametres in it) from which the user was redirected in the get method in the view associated with the url page3 ?
After reading the docs more thoroughly (thanks for the tip Brandon!), I found request.META['HTTP_REFERER'] did the trick.

To stop a redirect back to logout when the login URL ends with ?next=/accounts/logout/ in Django

In my template I am currently using the next parameter to redirect the user back to the page before the login page with this:
Log in
The firstof tag makes sure that in case request.path is invalid, then it will redirect back to the root URL.
This works well on every page except one: the logout page. If I wanted to switch user, then I would first log out, then click log in. But then my url would be
http://127.0.0.1:8000/accounts/login/?next=/accounts/logout/
So as soon as I log in, I would immediately be logged back out again. How do I modify the template so with something like this pseudocode:
if request.path and request.path != reverse( 'auth_logout' )
return request.path
return "/"
The easiest solution I can think of, is add "?next=/" to your logout url, that way, as soon as the user logs out, he will be immediately redirected to the specified url, so no one will ever stay on logout page upon logout.
Otherwise you would have to rewrite the login view and add any custom logic you need, which would be easier if contrib.auth views were class views, which they are currently not unfortunately.
So copy paste it and modify :) (I know that copy pasting is bad, but thats the only way you could add custom behavior to it).
The view is located here:
https://code.djangoproject.com/browser/django/trunk/django/contrib/auth/views.py#L25

How to redirect to page which has GET parameters after login using the {{next}} variable in django

I am using allauth to provide registration and login in my django site. Everything else seems to be working fine other than that I am having problems to redirect the person to the current page after login.
I have a page where I have some interview questions and a typical url for it would be like
/questions/?company=google
This page contains a list of questions for the company google, but to view the answer the person needs to login. The answers are displayed in a dropdown box. However when the user clicks on login a request is sent to the login page as follows
/login/?next=/questions/
And the get parameter which was actually there in my actual page is not sent because of the & in my url. How can I solve this problem. It does not look nice that the person is redirected to a different page from where he/she tried to login.
I know sending the next parameter as a GET variable is not the solution, but is there a way I can send the redirect link as a POST variable from the template.
I tried another thing, in my view that displays the questions list. I set session variables which contains the url of the current link . If a user clicks on login, in my login view I check for this particular session variable. If it is set then I redirect to that page.
However the session variable is not received in the login view, I am not sure but I think the session is reset when the user goes to the login view.
Any suggestions are appreciated.
Have you tried
next = request.get_full_path()
This will return correct path with all queries ( see docs ) , you can then pass it as GET param to redirect url e.g.
full_path = request.get_full_path()
return HttpResponseRedirect('%s?next=%s' % (reverse('login'), full_path))
You should encode the URL-parameter in this case. You want to send a variable like /questions/?company=google, but as you mentioned the ?, = (amongst others) characters are special ones. It has a special meaning when embedded in the URL. If you encode the variable with URL encoding, it becomes %2Fquestions%2F%3Fcompany%3Dgoogle. If you assign that to the parameter next, the URL becomes: /login/?next=%2Fquestions%2F%3Fcompany%3Dgoogle. This should redirect to the correct place on login.

Django: Redirect to a page, then redirect back again?

As per the title: in Django views, can I redirect to a page using HttpResponseRedirect and then from that page, immediately redirect back again to the original page?
In other words, how can I get the second view to 'remember' the first one in order to redirect back there?
I want to do this to handle some LDAP authorisation.
Thanks!
You could redirect to /page2/?next=/page1/, then get the original url from the GET parameters in the view for page2.
# page2 viewl
next = request.GET['next']
return HttpResponseRedirect(next)
You probably want to avoid any session level logic. Your requirements have nothing to do with a session, so avoid using session level constructs.
You have a request level requirement, and the request level logic identified by Alasdair is what you want.
You could store the original URL in a session variable, and then pop off that value and use it to redirect back to the original page.