django-socialregistration¶
django-socialregistration enables developers to add user registration with OAuth, OAuth2 and OpenID services.
See documentation for installation and configuration.
Setup¶
Requirements¶
Note
The v0.5.x versions of django-socialregistration require at least Django v1.3.2 due to the use of class based views. For previous versions of Django please use django-socialregistration v0.4.x.
Installation¶
pip install django-socialregistration
pip install -e git+https://github.com/pythonforfacebook/facebook-sdk#egg=FacebookSDK
Configuration¶
The most basic configuration is to add socialregistration
and
django.contrib.sites
to your INSTALLED_APPS
INSTALLED_APPS = (
'django.contrib.sites',
'socialregistration'
)
Note
To make sure that your redirects and callbacks work properly you’ll have to set the domain in the Sites app to the correct value.
If you find yourself redirected to example.com, check your Sites configuration through the Django admin interface.
Include socialregistration.urls
into your root urls.py
file
urlpatterns = patterns('',
# Here be other urls ...
url(r'^social/', include('socialregistration.urls',
namespace = 'socialregistration')))
Note
The namespace = 'socialregistration'
argument is required.
Include django.core.context_processors.request
in your TEMPLATE_CONTEXT_PROCESSORS in your settings file
TEMPLATE_CONTEXT_PROESSORS = (
'django.core.context_processors.request',
)
Note
When your views render templates that include social registration tags (such as {% twitter_button %}) they will need to pass the RequestContext in as a parameter
return render_to_response('template.html', c, context_instance=RequestContext(request))
Note on sessions¶
When starting the registration process, all the user’s temporary data is stored
in the user’s session. If you’re developing on http://127.0.0.1:8000, you will
have to set your callback URLs to begin with http://127.0.0.1:8000 too or you will get
a new session when returning to the site and socialregistration won’t be able
to find the temporary data and subsequently throw a KeyError
.
Also not that Twitter for example will not accept http://localhost:8000 as a valid domain for the callback URL so you’ll have to use http://127.0.0.1:8000 when developing your site.
Services¶
Facebook¶
- Add
socialregistration.contrib.facebook
to yourINSTALLED_APPS
INSTALLED_APPS = (
'socialregistration',
'socialregistration.contrib.facebook'
)
- Add
socialregistration.contrib.facebook.auth.FacebookAuth
to yourAUTHENTICATION_BACKENDS
AUTHENTICATION_BACKENDS = (
'django.contrib.auth.backends.ModelBackend',
'socialregistration.contrib.facebook.auth.FacebookAuth',
)
- Add your API keys and (comma seperated) permissions you request:
FACEBOOK_APP_ID = ''
FACEBOOK_SECRET_KEY = ''
FACEBOOK_REQUEST_PERMISSIONS = ''
- Anywhere in your templates:
{% load facebook %}
{% facebook_button %}
Or:
{% load facebook %}
{% facebook_button 'custom/button/image.png' %}
Foursquare¶
- Add
socialregistration.contrib.foursquare
to yourINSTALLED_APPS
INSTALLED_APPS = (
'socialregistration',
'socialregistration.contrib.foursquare'
)
- Add
socialregistration.contrib.foursquare.auth.FoursquareAuth
to yourAUTHENTICATION_BACKENDS
AUTHENTICATION_BACKENDS = (
'django.contrib.auth.backends.ModelBackend',
'socialregistration.contrib.foursquare.auth.FoursquareAuth',
)
- Add your API keys and (comma seperated) permissions you request:
FOURSQUARE_CLIENT_ID = ''
FOURSQUARE_CLIENT_SECRET = ''
FOURSQUARE_REQUEST_PERMISSIONS = ''
- Anywhere in your templates:
{% load foursquare %}
{% foursquare_button %}
Or:
{% load foursquare %}
{% foursquare_button 'custom/button/image.png' %}
Github¶
- Add
socialregistration.contrib.github
to yourINSTALLED_APPS
INSTALLED_APPS = (
'socialregistration',
'socialregistration.contrib.github'
)
- Add
socialregistration.contrib.github.auth.GithubAuth
to yourAUTHENTICATION_BACKENDS
AUTHENTICATION_BACKENDS = (
'django.contrib.auth.backends.ModelBackend',
'socialregistration.contrib.github.auth.GithubAuth',
)
- Add your API keys and (comma seperated) permissions you request:
GITHUB_CLIENT_ID = ''
GITHUB_CLIENT_SECRET = ''
GITHUB_REQUEST_PERMISSIONS = ''
- Anywhere in your templates:
{% load github %}
{% github_button %}
Or:
{% load github %}
{% github_button 'custom/button/image.png' %}
LinkedIn¶
- Add
socialregistration.contrib.linkedin
to yourINSTALLED_APPS
INSTALLED_APPS = (
'socialregistration',
'socialregistration.contrib.linkedin'
)
- Add
socialregistration.contrib.linkedin.auth.LinkedInAuth
to yourAUTHENTICATION_BACKENDS
AUTHENTICATION_BACKENDS = (
'django.contrib.auth.backends.ModelBackend',
'socialregistration.contrib.linkedin.auth.LinkedInAuth',
)
- Add your API keys:
LINKEDIN_CONSUMER_KEY = ''
LINKEDIN_CONSUMER_SECRET_KEY = ''
- Anywhere in your templates:
{% load linkedin %}
{% linkedin_button %}
Or:
{% load linkedin %}
{% linkedin_button 'custom/button/image.png' %}
OpenID¶
- Add
socialregistration.contrib.openid
to yourINSTALLED_APPS
INSTALLED_APPS = (
'socialregistration',
'socialregistration.contrib.openid'
)
- Add
socialregistration.contrib.openid.auth.OpenIDAuth
to yourAUTHENTICATION_BACKENDS
AUTHENTICATION_BACKENDS = (
'django.contrib.auth.backends.ModelBackend',
'socialregistration.contrib.openid.auth.OpenIDAuth',
)
- Anywhere in your templates:
{% load openid %}
{% openid_form %}
Or
{% load openid %}
{% openid_form 'https://www.google.com/accounts/o8/id' 'login/with/google.png' %}
Tumblr¶
- Add
socialregistration.contrib.tumblr
to yourINSTALLED_APPS
INSTALLED_APPS = (
'socialregistration',
'socialregistration.contrib.tumblr'
)
- Add
socialregistration.contrib.tumblr.auth.TumblrAuth
to yourAUTHENTICATION_BACKENDS
AUTHENTICATION_BACKENDS = (
'django.contrib.auth.backends.ModelBackend',
'socialregistration.contrib.tumblr.auth.TumblrAuth',
)
- Add your API keys:
TUMBLR_CONSUMER_KEY = ''
TUMBLR_CONSUMER_SECRET_KEY = ''
- Anywhere in your templates:
{% load tumblr %}
{% tumblr_button %}
Or:
{% load tumblr %}
{% tumblr_button 'custom/button/image.png' %}
Twitter¶
- Add
socialregistration.contrib.twitter
to yourINSTALLED_APPS
INSTALLED_APPS = (
'socialregistration',
'socialregistration.contrib.twitter'
)
- Add
socialregistration.contrib.twitter.auth.TwitterAuth
to yourAUTHENTICATION_BACKENDS
AUTHENTICATION_BACKENDS = (
'django.contrib.auth.backends.ModelBackend',
'socialregistration.contrib.twitter.auth.TwitterAuth',
)
- Add your API keys:
TWITTER_CONSUMER_KEY = ''
TWITTER_CONSUMER_SECRET_KEY = ''
- Anywhere in your templates:
{% load twitter %}
{% twitter_button %}
Or:
{% load twitter %}
{% twitter_button 'custom/button/image.png' %}
API¶
Mixins¶
Signals¶
Socialregistration comes with two signals that you can subscribe to:
The
connect
signal is fired every time a new social profile of sorts is created. The signal handler should accept four arguments. An additional fifth, the current request object, is passed in too:from socialregistration import signals from socialregistration.contrib.facebook.models import FacebookProfile def connect_handler(sender, user, profile, client, request = None, **kwargs): data = client.graph.request('me') signals.connect.connect(connect_handler, sender = FacebookProfile, dispatch_uid = 'facebook.connect')
The
login
signal is fired every time a user signs into your Django application via a third party API. The signal handler here should also accept four arguments and take an optional fifth argument, being the current request:def login_handler(sender, user, profile, client, request = None, **kwargs): data = client.graph.request('me') signals.login.connect(login_handler, sender = FacebookProfile, dispatch_uid = 'facebook.connect')
Tests¶
Views¶
Settings¶
Default: False
Whether your callback URLs are served via SSL.
Default: 'socialreg:'
The session key prefix to store temporary user information with while the user is setting up an account.
Default: False
If set to
True
the username will be generated automatically
Default: 'socialregistration.utils.generate_username'
If
SOCIALREGISTRATION_GENERATE_USERNAME
isTrue
, this function will be called and should return a username. The function will receive three arguments:- A user model
- A profile model
- An API client
Default: 'socialregistration.forms.UserForm'
IF
SOCIALREGISTRATION_GENERATE_USERNAME
isFalse
, this form will be used to capture data from the user, such as username and email address.
Default: None
A function that should return some initial data for the
SOCIALREGISTRATION_SETUP_FORM
. The function takes four parameters:- The request object
- A user model
- A profile model
- An API client
socialregistration Package¶
socialregistration
Package¶forms
Module¶mixins
Module¶utils
Module¶socialregistration.utils.
generate_username
(user, profile, client)[source]¶Default function to generate usernames using the built in uuid library.
views
Module¶Subpackages¶
clients Package¶
clients
Package¶oauth
Module¶templatetags Package¶
templatetags
Package¶socialregistration.templatetags.
ButtonTag
(template_name, params=[])[source]¶Bases:
django.template.base.Node
render
(context)[source]¶socialregistration.templatetags.
button
(template_name)[source]¶socialregistration.templatetags.
get_bits
(token)[source]¶socialregistration.templatetags.
resolve
(what, context)[source]¶socialregistration.contrib
Packages¶facebook Package¶
auth
Module¶client
Module¶middleware
Module¶models
Module¶views
Module¶foursquare Package¶
auth
Module¶client
Module¶models
Module¶views
Module¶github Package¶
auth
Module¶client
Module¶models
Module¶views
Module¶linkedin Package¶
auth
Module¶client
Module¶models
Module¶views
Module¶openid Package¶
auth
Module¶client
Module¶models
Module¶storage
Module¶views
Module¶tumblr Package¶
auth
Module¶client
Module¶models
Module¶views
Module¶twitter Package¶
auth
Module¶client
Module¶models
Module¶views
Module¶