social login, how to redirect to ?next= after login

3,276 views
Skip to first unread message

Stefano Tranquillini

unread,
Nov 22, 2012, 5:40:54 PM11/22/12
to django-so...@googlegroups.com
Hi.
i was pretty sure to have send a message today, but it does not show up in the list. So:
i've read that someone in the past had the same problem, but from that massage i was not able to figure out what to do.

basically, i've pages protected with login_required. if a user goes to one of that pages and he's not logged in it's redirected to my /login page. Django puts a ?next=XXX attached to /login where XXX is the page the user required.
Now, in the login page i've the button "login with facebook" like this 

<a href="{% url socialauth_begin 'facebook'  %}">Login with Facebook</a>

but once logged in the user is redirected to the LOGIN_REDIRECT_URL = '/' instead of the XXX page.

i guess this is beacues i've to pass the XXX in the reverse function. 
but how can i do that?
social_auth doesn't have a config for this?

thanks.

ciao

Matías Aguirre

unread,
Nov 22, 2012, 5:56:26 PM11/22/12
to django-social-auth
Hi Stefano,

Just add the next parameter to the href like this:

<a href="{% url socialauth_begin 'facebook' %}?next=XXX">Login with Facebook</a>

Matías

Excerpts from Stefano Tranquillini's message of 2012-11-22 20:40:54 -0200:
--
Matías Aguirre (matias...@gmail.com)

Stefano Tranquillini

unread,
Nov 23, 2012, 5:25:58 AM11/23/12
to django-so...@googlegroups.com
ok,
so, side question: how can i read the ?next=XXX from the url.
because i've the user wants /profile it will bet login?next=/profile if he wants /deals it will be login?next=/deals etc
how can i read that parameter in the template?
--
Stefano

Matías Aguirre

unread,
Nov 23, 2012, 11:21:34 AM11/23/12
to django-social-auth
The same way django does, request.get_full_path().

Excerpts from Stefano Tranquillini's message of 2012-11-23 08:25:58 -0200:
Matías Aguirre (matias...@gmail.com)

Stefano Tranquillini

unread,
Nov 24, 2012, 5:58:25 AM11/24/12
to django-so...@googlegroups.com
just for refernece
this is what i used in the template

<a href="{% url socialauth_begin 'facebook'  %}?next={{ request.GET.next }}">Login with Facebook</a>

you have also to add this to the setting (in Template context processors)

    'django.core.context_processors.request',


ciao

--
Stefano

Jeroen van Rijn

unread,
Dec 21, 2012, 12:58:21 AM12/21/12
to django-so...@googlegroups.com
Hi,

The ?next=<url> seems to work if the user already had a profile on the site, but when the user is new, it appears the ?next=<url> gets ignored and the user is forwarded to the SOCIAL_AUTH_NEW_USER_REDIRECT_URL

Is there a way around this? I really want even new users to end up on the url indicated by ?next=<url>

Thanks!

Jeroen

Matías Aguirre

unread,
Dec 21, 2012, 6:19:52 PM12/21/12
to django-social-auth
Do you have SOCIAL_AUTH_NEW_USER_REDIRECT_URL defined? Do you need some users
to land there, but others to land on the value on next?

Excerpts from Jeroen van Rijn's message of 2012-12-21 03:58:21 -0200:
--
Matías Aguirre (matias...@gmail.com)

Jeroen van Rijn

unread,
Dec 21, 2012, 6:35:06 PM12/21/12
to django-so...@googlegroups.com
Matias,

Exactly! I actually just solved it. I just have everyone get to SOCIAL_AUTH_NEW_USER_REDIRECT_
URL and in the view for that URL I test for the presence of 'next' in request.session and take action accordingly.

Thanks for making and supporting the code!

Jeroen
Reply all
Reply to author
Forward
0 new messages