3

I'm using django-social-auth with Django 1.3.1 under Python 2.7.1. I've been looking all over the place and I can't quite figure out how to use a custom redirection url for Twitter Authentication (or other providers).

I've read that I need to use this template tag in my html template to build the link to initiate the process:

{% url socialauth_begin 'twitter' %}

This works relatively fine using the value in my settings.py file for the LOGIN_REDIRECT_URL (although, see my question here: Twitter takes users to /en/logged-in/ regardless of my settings) However, in this particular case, I need to redirect the user to a special url upon return.

I've read in other places that I need to pass the variable 'next' with my redirect value to the template or as a parameter on the twitter auth url. How is this done? My template is loaded as a HttpResponse, so, while I can certainly pass it template values, it isn't clear how I can use these in the above template tag.

Sorry for this basic question.

Community
  • 1
  • 1
mkoistinen
  • 7,724
  • 3
  • 41
  • 56

2 Answers2

7

OK, I knew this was simple, and I feel a bit silly for even asking this one. I guess I thought that django-social-auth had special provisions for doing this, but apparently, it's pretty manual. Here's the solution for anyone else who ends up here:

In your view: ... from django.utils.http import urlquote ...

def my_view(request, ...):
  ...
  redirect_url = urlquote("http://your_required_redirect_url/")
  ...
  return render_to_response('your_template.html', { 'redirect_url' : redirect_url })

In 'your_template.html':

...
Login with <a href="{% url socialauth_begin 'twitter' %}?next={{ redirect_url }}">Twitter</a>
...
mkoistinen
  • 7,724
  • 3
  • 41
  • 56
0

I'm using this code:

<a href="{% url socialauth_begin 'twitter' %}?next={{ request.get_full_path }}">Login with Twitter</a>

this line of code makes redirect to the same page, you can even set it to your header.

Stepan
  • 86
  • 1
  • 5