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.
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.
- Managing multiple settings.py files
- How can I best find out how django works internally?
- Django 2.0: sqlite IntegrityError: FOREIGN KEY constraint failed
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.