Writing a Facebook App using Django

I recently found myself writing my first Facebook app using Python with the Django web framework. I have to admit that it was not a very pleasant experience. The usual Django development cycle of programming and testing locally does not work. I have a very simple workflow nailed down when writing Django apps: write, test locally, commit to VCS, and upload to server. When writing a Facebook app, I had to change the current workflow a bit: write, commit to VCS, upload to server, test on production.

Facebook, being a PHP-powered site, likes to make the assumption that you will also be writing your app using PHP. They don’t even make any efforts to hide their bias towards PHP. The “official” client library is for PHP. The example program they give to you the first time you sign up is written in PHP. All the relevant starting points all lead towards PHP-centric development.

Thankfully, there are those who realize that PHP is not the be-all and end-all to programming web applications. You can find unofficial client libraries for other programming languages. Download and install the client library for your programming language of choice and follow the documentation and hopefully you get to have everything working on your first try.

If you are programming a web-based Facebook app in Python, you will have two choices:

  1. PyFacebook – a little bit on the heavy side since it comes with a framework-neutral core plus Django integration and Google App Engine support. It seems to be more actively maintained. The latest revision was on December 2, 2008 at the time of writing.

  2. minifb – small. However it seems that activity has stopped since February 2008.

The choice of client library was a no-brainer for me since the site I am working on was written using Django. After downloading and installing PyFacebook I was able to get a minimal Facebook app up and running. By “up and running” I mean I can view the app’s canvas page on Facebook. However updates to my profile via profile.setFBML() do not seem to be working. After a bit of digging, I was able to find the culprit in a bug report. The modifications suggested by the issue reporter fixed the problem.

The app I was making was supposed to post updates to a Facebook Page. Facebook Pages are a relatively new feature and as a result documentation for it is a bit lacking. The IRC transcript in the Wiki page for Facebook Pages provided some useful clues. There are two things you need to ensure if you need to write a Django view that has to update a Facebook Page:

  1. Do not use @facebook.require_login() decorator.

  2. The uid parameter for the profile.setFBML() call must be taken from the fb_page_id query parameter.

For example:

from django.template import loader, RequestContext

def update_page_profile(request):
    # facebook attribute in request is added by FacebookMiddleware
    uid = request.GET.get('fb_page_id')
    if uid:
        ctx = RequestContext(request, {'page_id': uid})
        # Do whatever you need to do here...
        t = loader.get_template('facebook/page_fbml.xml')
        # Render the template into the profile parameter for setFBML.
        # We do not use the deprecated markup parameter.
        request.facebook.profile.setFBML(profile=t.render(ctx), uid=uid)
        # Return a redirect to the profile URL for the page.
        # Note that we do not use Django HttpResponseRedirect here.
        return request.facebook.redirect(request.facebook.get_url('profile', id=uid))
    else:
        # Do something else here if the view did not get called
        # with the fb_page_id parameter. One possibility is to check if we got
        # called with a valid user ID and do something completely different.
        pass

One other thing to note about “profile boxes” in Facebook Pages is that they do not automatically update every time you view the Facebook Page. That is, you will need to provide a way of calling the update view manually. What I did was set the update view URL as the “Page Edit URL” for the app when embedded in a Facebook Page. Ideally you should set “Page Edit URL” to output a canvas page where the page admin can update settings for the app. Because the Facebook app I was making was mainly intended for internal use, I opted out of having a dedicated “Page Edit” canvas page and instead linked it directly to the profile update view URL.

One Response to “Writing a Facebook App using Django”

  1. lims Says:

    The link to PyFacebook is wrong.. it points to minifb

Leave a Reply

Comments are moderated by the administrator. If this is your first time posting a comment, your comment will go to a moderation queue and it may take a while for your comment to appear. Or it may get deleted.