MellowMorning
  • About
  • Twitter
  • LinkedIn
Css

Django Facebook 2.0 – Integrating Facebook

By Thierry Schellenbach June 23, 2011 4 Mins Read
Share
Share on Facebook Share on Twitter Pinterest Email

It’s been a year since Facebook changed the web with the open graph API and their like button. Unfortunately Facebook didn’t have much developer love for the python/django platform. Django Facebook aims to make it easy to build facebook apps and integrate facebook with your website.

Key functionality

One of the strong points of Django Facebook is the ability to register users using Facebook. It ports all of Facebook’s user data to the Django user and profile models. This allows you to have a secure register/connect/login flow using Facebook, greatly reducing the barriers to start using your application. Below an example of me registering for Fashiolista using the Facebook register flow.

Me on Facebook

Me on Fashiolista

In this blog post I will explain how to get started implementing a Facebook connect flow. Django Facebook can however do quite a bit more, as you can see in the feature list below. Development over at the github repo is very active. I strongly appreciate help on improving the functionality so please fork and contribute.
Features

  • Register users using the Facebook open graph API
    • Full profile data birthday, gender, website, about me, username, email and picture
    • Support for mobile authentication
    • Fallback to registration form for entering additional data when required
  • Build Facebook Canvas applications
  • Mobile registration using Facebook
  • Execute Facebook FQL queries
  • Upload pictures to Facebook
  • Find your Facebook friends

Getting Started

Now let’s get started with building a Facebook login/connect/register flow. This post will guide you through it step by step.

Prerequisites

Create a Facebook app
Have django registration installed (other registration systems will require some small code changes)
pip install django_facebook πŸ™‚

A.) Settings and more

Define these settings in your settings file.
FACEBOOK_API_KEY
FACEBOOK_APP_ID
FACEBOOK_APP_SECRET
add django facebook to your installed apps
‘django_facebook’,
add this line to your url config
(r’^facebook/’, include(‘django_facebook.urls’)),
add this line to your context processors
‘django_facebook.context_processors.facebook’,
add this to your AUTHENTICATION_BACKENDS
‘django_facebook.auth_backends.FacebookBackend’,

B.) Adjust your user Profile model

Secondly we need to be able to store the Facebook data on your user profile.
The easiest way to do this is to add the abstract model in django_facebook/models.py called FacebookProfileModel to your profile model.
After your profile is ready to store Facebook data you should have a working example at /facebook/connect/.
Let me know in the comments if something went wrong up to this point πŸ™‚

C.) Design and integration

You can style the facebook form and button anyway you see fit. Over at Fashiolista we added a nice facepile for example. The basic markup is located in the example file connect.html.
We use the facebook javascript SDK for a smooth user integration. You can load the facebook JS like this:

<script src="{{ MEDIA_URL }}js/original/facebook.js" type="text/javascript"></script>
<script>
facebookAppId = '{{ FACEBOOK_APP_ID }}';
function facebookJSLoaded(){
FB.init({appId: facebookAppId, status: false, cookie: true, xfbml: true});
}
window.fbAsyncInit = facebookJSLoaded;
F = new facebookClass(facebookAppId);
F.load();
</script>

Subsequently implement a form which calls Facebook via javascript. Note that you can control which page to go to after connect using the next input field.

<form action="{% url facebook_connect %}?facebook_login=1" method="post">
<a href="javascript:void(0);" style="font-size: 20px;" onclick="F.connect(this.parentNode);">Register, login or connect with facebook</a>
<input type="hidden" value="{{ request.path }}" name="next" />
</form>
<div id="fb-root"></div>

That was all, you should now have a working registration flow using Facebook. Let me know in the comments if you encounter any difficulties.
If you want to go one step further and understand the facebook API, you can find the Facebook documentation here.
Django Facebook received tons of improvements from the python community. I’m certainly missing a few authors, but I would like to thank a few specifically:
Many thanks to (amongst many others)

  • Kenneth Love
  • Greg Taylor
  • Riccardo Magliocchetti
  • Mikhail Korobov
  • Simple Geebus
  • Jon Rohan
  • Rick van Hattem
  • Stochastic Technologies
  • Simplegeo
  • Can Burak Cilingir
  • zdexter
  • Michael Robellard
  • Ramiro Morales

More posts coming up

  • Building a facebook canvas app using Django Facebook
  • Find and invite friends using Django Facebook

We’re hiring!

Do you also see the beauty in clean code? Are you experienced with high scalability web apps? Currently we’re looking for additional talent over at our Amsterdam office. Feel free to drop me a line at my personal email for more information: thierryschellenbach[at]gmail.com

Djangodjango facebookFacebookFacebook connectFacebook loginFacebook registrationfacebook sdkopen graphPythonSocial authentication
0
Author Thierry Schellenbach

Related Posts

Twitter way back machine

March 24, 2015

How to build a notification feed using Stream

March 6, 2015

Python 3 – Stream Framework

March 3, 2015

Build a scalable Twitter clone with Django and GetStream.io

December 4, 2014
  • About Thierry

    Thierry Schellenbach the is co-founder and CEO of Stream. Stream is an enterprise-grade chat and activity feed API that serves more than a billion end-users.

    He’s the author of several widely used open-source software packages, including Stream Framework, Django Facebook and a popular open-source RSS reader called Winds.

    Twitter
  • Latest Posts
    • Java/Kotlin Android Chat Tutorial
      August 27, 2019
    • Swift/iOS Chat Tutorial with Stream Chat
      July 30, 2019
    • React Native Chat
      June 5, 2019

Β© 2020 Thierry Schellenbach

Top

    Type above and press Enter to search. Press Esc to cancel.