Skip to content

mxfunc/Django-facebook

 
 

Repository files navigation

Django Facebook by Thierry Schellenbach (http://www.mellowmorning.com)

First

Please contribute code :)

Contact me here or @tschellenbach Check my blog for updates mellowmorning

About Django Facebook

Django Facebook allows you to connect to the Facebook Open Graph API. Integrated with Django it becomes easy to setup a login/register via Facebook flow for your users.

Features

  • Access the Facebook API, from:
    • Your website (Using javascript OAuth)
    • Facebook canvas pages (For building facebook applications)
    • Mobile (Or any other flow giving you a valid access token)
  • Django User Registration (Convert Facebook user data into a user model)
  • Use Facebook data to register a user with your Django app. Facebook connect using the open graph API.
  • Facebook FQL access
  • OAuth 2.0 compliant
  • Includes Open Facebook (stable and tested python client to the graph API)

Requirements

  • Django registration or Django Userena (contact me if you use something else for registration.)
  • Django 1.3

Running tests

Tests are run from within the example project. You can run them yourself as follows:

install from git

facebook_example/manage.py test django_facebook

Installation

Download the source code or use pip install django_facebook.

Create a Facebook App

In case you don't yet have a facebook app. You need an app to use the open graph api and make the login process work. You can create a facebook app at this url: http://www.facebook.com/developers/createapp.php

Settings

Define the following settings in your settings.py file:

FACEBOOK_APP_ID
FACEBOOK_APP_SECRET

Url config, context processor, auth backend

add django facebook to your installed apps:

'django_facebook',

add this line to your context processors (TEMPLATE_CONTEXT_PROCESSORS setting):

'django_facebook.context_processors.facebook',

add this to your AUTHENTICATION_BACKENDS setting:

'django_facebook.auth_backends.FacebookBackend',

add this line to your url config:

(r'^facebook/', include('django_facebook.urls')),

Update your models

An abstract model is specified here django_facebook/models.py Add these fields to your profile model or subclass the abstract class. See the django docs for settings up a Profile model or understanding abstract classes.

Check the example

Right now you should have a working registration/connect/login in flow available at /facebook/connect/ Test if everything is working and ensure you didn't miss a step somewhere. If you encounter any difficulties please open an issue.

** Django Userena **

A few settings changes are needed to play nicely with Django Userena. In your settings point Django Facebook to the right registration form and template:

FACEBOOK_REGISTRATION_FORM = 'userena.forms.SignupForm' FACEBOOK_REGISTRATION_TEMPLATE = 'userena/signup_form.html'

Supporting any other registration system is quite easy. Adjust the above settings to point to your own code. Note that the form's save method needs to return the new user object.

Common bugs

Django Facebook expects that you are using static files in order to load the required javascript. If you are not using staticfiles you should load facebook.js provided in the static directory manually.

Another common issue are the url matching settings from Facebook. Facebook requires you to fill in a domain for your application. In order for things to work with local development you need to use the same domain. So if you production site is www.mellowmorning.com you should run your development server on something like local.mellowmorning.com in order for facebook to allow authentication.

If you encounter any difficulties please open an issue.

Customize and integrate into your site

This is the hardest step of the install. For an example you can look at connect.html in the templates directory.

First load the javascript (it loads the facebook library asynchronously). I recommend that you insert this code at the bottom of your page.

<script src="{{ STATIC_URL }}js/facebook.js" type="text/javascript"></script>
<script>
facebookAppId = '{{ FACEBOOK_APP_ID }}';
facebookDefaultScope = {{ FACEBOOK_DEFAULT_SCOPE_JS }};
function facebookJSLoaded(){
FB.init({appId: facebookAppId, status: false, cookie: true, xfbml: true, oauth: 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>

On a final note the Facebook javascript SDK requires you to place a 'fb-root' div somewhere on your page. (See the example).

Optional Settings

If you want to use a different registration form template for Facebook connect:

FACEBOOK_REGISTRATION_TEMPLATE = 'django_facebook/facebook_registration_form.html'

Django Jobs

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

About

Facebook open graph api implementation using the Django web framework in python

Resources

License

Stars

Watchers

Forks

Packages

No packages published