Python Social Auth Django template example

Filipe Ferminiano picture Filipe Ferminiano · Feb 24, 2014 · Viewed 15.4k times · Source

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).

Answer

baltasvejas picture baltasvejas · Jun 27, 2014

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.