Setting up Django-Allauth

Well, we are now in the project phase and I’ve been looking for a solution to using authentication with my Django web application and after many days of research, I’ve finally settled on django-allauth,

‘”Why?”, you ask? Well, it was the only one I was able to figure out.

Because I am a new developer, I tend to over think things, and setting up django-allauth took me a few days to get working but looking back on it now, it’s not all that hard. The documentation on Github is great, for the most part, but I found that there are some bits that were unclear so here is what I did to get going.

First you need to install django-allauth

pip install django-allauth

One you have everything installed, go into your project directory.

$ cd documents/list_hub

A little side note: The django-allauth package has it’s own templates which is super handy, eventually you may want to customize them but in the meantime just grab the source code from Github and copy just the templates directory into your project directory and customize those files later if you are so inclined.

Once you’re inside your project directory, find your settings.py file and input the following:

TEMPLATE_CONTEXT_PROCESSORS = (
    # This is required by allauth template tags
    "django.core.context_processors.request",
    # These are allauth specific context processors
    "allauth.account.context_processors.account",
    "allauth.socialaccount.context_processors.socialaccount",

)

AUTHENTICATION_BACKENDS = (
    # Needed to login by username in Django admin, regardless of `allauth`
    "django.contrib.auth.backends.ModelBackend",
    # `allauth` specific authentication methods, such as login by e-mail
    "allauth.account.auth_backends.AuthenticationBackend",
)

INSTALLED_APPS = (
    # The Django sites framework is required
    'django.contrib.sites',
    'allauth',
    'allauth.account',
    'allauth.socialaccount',
    # ... include the providers you want to enable:
    'allauth.socialaccount.providers.amazon',
    'allauth.socialaccount.providers.angellist',
    'allauth.socialaccount.providers.bitbucket',
    'allauth.socialaccount.providers.bitly',
    'allauth.socialaccount.providers.dropbox',
    'allauth.socialaccount.providers.facebook',
    'allauth.socialaccount.providers.flickr',
    'allauth.socialaccount.providers.feedly',
    'allauth.socialaccount.providers.github',
    'allauth.socialaccount.providers.google',
    'allauth.socialaccount.providers.instagram',
    'allauth.socialaccount.providers.linkedin',
    'allauth.socialaccount.providers.linkedin_oauth2',
    'allauth.socialaccount.providers.openid',
    'allauth.socialaccount.providers.persona',
    'allauth.socialaccount.providers.soundcloud',
    'allauth.socialaccount.providers.stackexchange',
    'allauth.socialaccount.providers.tumblr',
    'allauth.socialaccount.providers.twitch',
    'allauth.socialaccount.providers.twitter',
    'allauth.socialaccount.providers.vimeo',
    'allauth.socialaccount.providers.vk',
    'allauth.socialaccount.providers.weibo',
)

# Don't forget the SITE_ID (A model for storing the domain and name attributes of a Web site. 
#The SITE_ID setting specifies the database ID of the site object associated with that particular #settings file.)
SITE_ID = 1

Then add the allauth/templates directory to your settings.py file:

TEMPLATE_DIRS = (
# Put strings here, like "/home/html/django_templates" or "C:/www/django/templates".
# Always use forward slashes, even on Windows.
# Don't forget to use absolute paths, not relative paths.
)

Then in your urls.py file add the following lines:

urlpatterns = patterns('',
    (r'^accounts/', include('allauth.urls')),
)

While still in your Django root execute the command below in your terminal to create your database tables:

python manage.py syncdb

and then run:

python manage.py run server

Once your server is running, visit your admin pages (e.g. http://localhost:8000/admin/)

Look! Now you have Django setup with django-alluth! Since you’re here, jump into your Django admin and start configuring your app APIs. This should mimic the providers in the INSTALLED_APPS section of your settings.py file and if you have no idea what I am talking about then you probably haven’t set up your apps yet. Go ahead and do that, i’ll wait.

Each providers interface for registering apps is different but for right now we just need a couple of things, such as, the Client ID and the Secret Key (Don’t share these values with anyone! It’s secret for a reason) once you have those in hand you can add those values to the “social app” section here — in the top right hand corner you’ll see the “add social app” button, click the button, and select a provider from the list provided, if you don’t see the provider your’e looking for then you haven’t added it to the INSTALLED_APPS section in your settings.py file.

Screen Shot 2014-03-24 at 2.48.28 PM

Note: You should also swap out the default site (www.example.com) for localhost:8000, and add it to ‘sites’ in your Django admin panel

TADA!! You can now log in using your providers credentials.

derpy_tada__by_jonathanmdful-d5suzn2


  • Michelle Glauser

    I definitely could have used this a couple of weeks ago. My auth is still a mess, but I’ve no time right now to clean it up. I’m bookmarking this post, thanks!

  • randy

    When I try to make migrations with django 1.7, I get a permission denied by trying to create a migration in the library. Migrating without those gives me an Invalid Bases Error. Any suggestions?

  • Mazulo

    Amazing! You help me to find the solution about the templates. Thank you so much 😀