Adding Social Bookmarking To a Django App

July 14, 2007. Filed under django 72 python 56

When you build a website you'll probably eventually want to create some links to allow readers to easily submit your content to social bookmarking and news aggregation services. Fortunately it is pretty easy to add this functionality to your Django application.

Making the template

The template is the most complex part of this endeavour, but is still very simple (this should be a good sign). In the template I am assuming that you'll have be using generic views, and thus the content will be named 'object'. If you are using custom views you may have named your content containing variable something else. Also note that you'll need to have a get_absolute_url method for your object.

The template also needs access to your site, but we'll deal with making the site available to the template a bit later on.

Here is the template:

{% with object.get_absolute_url|urlencode as obj_url %}
{% with object.title|urlencode as obj_title %}
{% with site.domain|urlencode as obj_domain %}

<span><strong> Social Links: </strong></span>
  
<span class="social"><a href="http://reddit.com/submit?url=http://{{ obj_domain }}{{ obj_url }}&title={{ obj_title }}"> Reddit </a></span>

<span class="social"><a href="http://del.icio.us/post?url={{ obj_domain }}{{ obj_url }}&title={{ obj_title }}"> Del.icio.us </a></span>

<span class="social"><a href="http://www.stumbleupon.com/submit?url={{ obj_domain }}{{ obj_url }}&title={{ obj_title }}"> StumbleUpon </a></span>

<span class="social"><a href="http://digg.com/submit?phase=2&url={{ obj_domain }}{{ obj_url }}&title={{ obj_title }}"> Digg </a></span>

{% endwith %}
{% endwith %}
{% endwith %}

You'll notice they we are using the urlencode filter on all of the strings we build, this is important because we'll be passing them as arguments to the urls that serve as the public api for the social sites we are communicating with.

You'll want to include the above template snippet inside the template you are submitting to the social sites. I usually do it with an include statement:

{% include "myapp/social_submit.html" %}

Which will simply inject that social code into the template directly (much easier than manually pasting it into multiple templates).

The Template Context

As mentioned above we also need to have access to our Site instance. The site is needed because we have no other way to figure out our full url for our content. We use the get_absolute_url method to get everything except the domain name, but we still need to get the domain name somehow (this means that our Site domain name needs to be the full domain name we want our urls to include www.lethain.com, for example).

Writing the context is also easy. First, make a new file in your application folder (I named mine context.py) and add these lines to it:

from django.contrib.sites.models import Site
from django.conf import settings

def site_context(request):
    site = Site.objects.get(pk=settings.SITE_ID)
    return {
        'site' : site,
        }

And then add these lines to your settings.py file:

TEMPLATE_CONTEXT_PROCESSORS = (
    "django.core.context_processors.auth",
    "django.core.context_processors.debug",
    "django.core.context_processors.i18n",
    "django.core.context_processors.media",
    "yourproject.yourapp.context.site_context",
    )

And thats all there is to it. Feel free to ask if you have any questions.