18

Does someone has an open example using Python Social Auth with Django in templates?

I took a look in their Github repo, and in the django exmaple, there is nothing about how to deal with it in templates (e.g. doing login, logout, etc).

Filipe Ferminiano
  • 8,373
  • 25
  • 104
  • 174
  • 2
    Like the templates here https://github.com/omab/python-social-auth/tree/master/examples/django_example/example/templates ? – omab Feb 25 '14 at 16:12
  • 1
    @omab Could you please tell me where I can find how to use the template tags? Can't seem to grasp much as to when to use 'social:begin' and when to use 'social:complete'. Searched the repo, couldn't seem to find a template tags list. – shad0w_wa1k3r Jun 09 '14 at 02:12
  • 1
    You shouldn't use `social:complete` except on a few cases (like when using a JS SDK), `social:begin` is the URL to start the auth process (login or signup, doesn't matter). There are a couple of template context processors to ease the creation of lists of already associated and not associated backends, docs for them are at http://psa.matiasaguirre.net/docs/configuration/django.html#template-context-processors – omab Jun 09 '14 at 05:38

3 Answers3

36

Let’s say you followed Python Social Auth configuration guidelines at http://psa.matiasaguirre.net/docs/configuration/django.html and you want using facebook login. Your backend settings in settings.py should look:

AUTHENTICATION_BACKENDS = (
'social.backends.facebook.FacebookOAuth2',
'django.contrib.auth.backends.ModelBackend',
)

You should register as facebook developer and create an app and then fill in additional data in your settings.py file:

SOCIAL_AUTH_FACEBOOK_KEY = 'xxxxxxxxxxxxxx'
SOCIAL_AUTH_FACEBOOK_SECRET = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'
SOCIAL_AUTH_FACEBOOK_SCOPE = ['email']

Let us assume after login you want users to be redirected to members page, so you add this setting to your settings.py:

LOGIN_REDIRECT_URL = '/members'

Let’s say you created login_app in your django project as well as created your home view with home.html template and also created members view with members.html template (you should have your template directory working).

According to configuration guidelines our urls.py should look:

from django.conf.urls import patterns, include, url
from django.contrib import admin

urlpatterns = patterns('',
    url('', include('social.apps.django_app.urls', namespace='social')),
    url(r'^admin/', include(admin.site.urls)),
)

If we would try bla-bla-bla url with DEBUG=True settings, we would get an error:

Using the URLconf defined in your_project.urls, Django tried these URL patterns, in this order:
    ^login/(?P<backend>[^/]+)/$ [name='begin']
    ^complete/(?P<backend>[^/]+)/$ [name='complete']
    ^disconnect/(?P<backend>[^/]+)/$ [name='disconnect']
    ^disconnect/(?P<backend>[^/]+)/(?P<association_id>[^/]+)/$ [name='disconnect_individual']
    ^admin/
The current URL, bla-bla-bla/, didn't match any of these.

For a very simple test we need to add home view, members view and logout (login is already handled), so our updated urls.py should look:

from django.conf.urls import patterns, include, url
from django.contrib import admin

urlpatterns = patterns('',
    url('', include('social.apps.django_app.urls', namespace='social')),
    url(r'^admin/', include(admin.site.urls)),
    url(r'^$', 'login_app.views.home', name='home'),
    url(r'^members/', 'login_app.views.members', name='members'),
    url(r'^logout/$', 'login_app.views.logout', name='logout'),
)

Under our login_app directory we should have files (do not pay attention to *.pyc files and migrations folder is present because I use django 1.7b4 version):

login_app/
├── admin.py
├── __init__.py
├── __init__.pyc
├── migrations
│   └── __init__.py
├── models.py
├── tests.py
├── views.py
└── views.pyc

Our views.py should look like:

from django.shortcuts import render, redirect
from django.contrib.auth import logout as auth_logout

def home(request):
    context = {}
    template = 'home.html'
    return render(request, template, context)

def members(request):
    context = {}
    template = 'members.html'
    return render(request, template, context)

def logout(request):
    auth_logout(request)
    return redirect('/')

Other files (including models.py) we may leave without adding anything.

In order to login with facebook we should redirect your users to “login/facebook”. So you can just add this link or button where appropriate somewhere in your home.html template:

<a href="login/facebook">Login with facebook</a>

After this link is pressed (in case settings.py, urls.py, views.py are ok and your facebook app is configured well) users will be logged in with facebook and redirected to members page. If you login to django admin, you should be able to see new entry under [ Home › Default › User social auths ] and new user in [ Home › Authentication and Authorization › Users ].

When user is authenticated and redirected to members page, you can have user’s information such as username, first name, last name, e-mail. You can display that information by adding to your members.html template:

<p>User's name and surname: {{ user.first_name }} {{ user.last_name}}</p>
<p>Username: {{ user.username }}</p>
<p>E-mail: {{ user.email }}</p>

As you already noticed, for logout we made an app in our views.py:

def logout(request):
    auth_logout(request)
    return redirect('/')

So we can add a link in our members.html template:

<a href="/logout">Logout</a>

And that would be enough to log out the user and redirect to initial home page.

This would be very simple example which may give a better understanding how to login and logout with Python Social Auth.

baltasvejas
  • 1,698
  • 2
  • 17
  • 18
  • 1
    much much more useful then https://github.com/omab/python-social-auth/blob/master/examples/django_example/example/templates/home.html – Robert Johnstone Mar 20 '15 at 15:28
13

Inside the python-social-auth there is an example. You just need to install python-social-auth, configure your database and your facebook app or another social app and put your Secret and key in settings.py file and run the application. There is a template with template tags and much more. Click here: https://github.com/omab/python-social-auth and see examples folder.

darxtrix
  • 2,032
  • 2
  • 23
  • 30
Bruno Paulino
  • 5,611
  • 1
  • 41
  • 40
  • omab's one has been replaced by python-social-auth which has got an example for django but it's all ajax and runs at 440 lines `https://github.com/omab/python-social-auth/blob/master/examples/django_example/example/templates/home.html`?!?! There is also another example cryptically called `django_me_example` in the examples folder. What does the 'me' mean? – Robert Johnstone Mar 20 '15 at 15:27
  • can you guide me to put key and secret of twitter in settings file – Jameel Grand Apr 30 '15 at 04:00
7

I found this tutorial most helpful: http://www.artandlogic.com/blog/2014/04/tutorial-adding-facebooktwittergoogle-authentication-to-a-django-application/ The example application in python-social-auth is comparatively terse and difficult to follow.

Unoti
  • 1,283
  • 11
  • 12
  • Your answer contains a link, which would be useless if the site goes inactive at some point in the future. Please consider adding some content from that website such that the readers could benefit from it. – Nisarg Shah Jul 24 '17 at 05:17
  • ...aaaaaand it's gone. – blimpse Aug 30 '21 at 20:26