There is this really nice plugin for WordPress called sociable that makes it easy to make links to various social bookmarking sites. There did not seem to be anything similar in Django, so I made a clone. It does not do everything that the sociable plugin does yet, but it is incredibly flexible. This code is licensed under LGPL.

You can get the latest version here or you can get the whole repository with mercurial:

hg clone http://code.tylerlesmann.com/django-sociable

You will probably want the sociable images also. You can get them from here.

Install is like many other python packages:

python setup.py install

Using django-sociable is easy. After install, edit your settings.py. You will need to specify a MEDIA_URL, if you have not already. Now add SOCIABLE_IMAGE_PATH, which is the path relative to the MEDIA_URL to find the sociable images. The last thing to add is put sociable into your INSTALLED_APPS.

The next step is defining a Site because django-sociable uses this to construct your submission link. You do not want it pointing to example.com. The easiest way to do this is from the Django admin site.

You are ready to use the django-sociable templatetags in your templates. Here's an example of its use from this very blog:

<div>
{% load sociable_tags %}
{% get_sociable Reddit Digg as sociable_links with url=object.get_absolute_url title=object.headline %}
{% for link in sociable_links %}
    <a href="{{ link.link }}">
        <img alt="{{ link.site }}" title="{{ link.site }}" src="{{ link.image }}" />
    </a>
{% endfor %}
</div>

Load sociable_tags. Use the get_sociable tag to get the information you need to build the links. Specify the sites to target, then the variable to map the information to, and finally the arguments. The only required argument is url, which should be a context variable containing link to submit. You can also submit title, excerpt, and source. Use the list of information from get_sociable in whatever way you like. Each element of the list has three attributes. The site attribute is the name of the target site. The link contains the full submission link. The image is the absolute path to the image corresponding to the site.

Here is a full list of the acceptable target sites:

BarraPunto blinkbits BlinkList BlogMemes blogmarks Blogsvine blogtercimlap Faves Book.mark.hu Bumpzee co.mments connotea del.icio.us De.lirio.us Digg DotNetKicks DZone eKudos email Facebook Fark feedmelinks Furl Fleck GeenRedactie Google Gwar Haohao HealthRanker Hemidemi IndianPad Internetmedia kick.ie Kirtsy laaik.it Leonaut LinkArena LinkaGoGo LinkedIn Linkter Live Ma.gnolia Meneame MisterWong MisterWong.DE Mixx muti MyShare MySpace N4G Netvibes NewsVine Netvouz NuJIJ Ping.fm PlugIM Pownce ppnow Print Propeller Ratimarks Rec6 Reddit SalesMarks Scoopeo scuttle Segnalo Shadows Simpy Slashdot Smarking Socialogs Spurl SphereIt Sphinn StumbleUpon Symbaloo Taggly Technorati TailRank ThisNext Tipd Tumblr TwitThis Upnews Webnews.de Webride Wikio Wists Wykop Xerpi YahooBuzz YahooMyWeb YCombinator Yigg

Posted by Tyler Lesmann on March 9, 2009 at 18:16
Tagged as: django new_features open_source python
Comments
#1 Yashh wrote this 5 years, 5 months ago

Have been following your blog for a while and it has awesome content on django. Love it. Glad you added comments as well.

Cool

#2 Tyler Lesmann wrote this 5 years, 5 months ago

I'm glad I can help. I'll try to keep the Django content coming. :)

#3 Skylar Saveland wrote this 5 years, 3 months ago

Very nice; I might try to use this over addthis in the future.

#4 Mike Korobov wrote this 5 years, 3 months ago

This app is very useful.
I need to add some Russian social websites to it so I created a repo on bitbucket:
http://bitbucket.org/kmike/django-sociable/

#5 Tyler Lesmann wrote this 5 years, 3 months ago

Sounds awesome. You've already done tons of work on it from what I've seen. :)

#6 Parag Shah wrote this 4 years, 11 months ago

Thanks, I was looking for something exactly like this for my open learning website.

#7 Parag Shah wrote this 4 years, 10 months ago

I tried getting the code using
hg clone http://code.tylerlesmann.com/django-sociable/

However, I get an error message:

"abort: 'http://code.tylerlesmann.com/django-sociable/' does not appear to be an hg repository!"

#8 Parag Shah wrote this 4 years, 10 months ago

Got it from
hg clone http://bitbucket.org/kmike/django-sociable/

Thanks

#9 Nicolas wrote this 3 years, 7 months ago

I had problems while passing unicode title strings to your app, here's a small patch that fixed the issue (I hope the display works here):

diff --git a/sociable.py b/sociable.py
index 72bbd2e..8e265bd 100644
--- a/sociable.py
+++ b/sociable.py
@@ -15,13 +15,13 @@ def genlink(target_site, **kwargs):
}
if 'url' not in kwargs:
raise TypeError, 'url Keyword argument is required'
- values['url'] = quote_plus(kwargs['url'])
+ values['url'] = quote_plus(kwargs['url'].encode("utf-8"))
if 'title' in kwargs:
- values['title'] = quote_plus(kwargs['title'])
+ values['title'] = quote_plus(kwargs['title'].encode("utf-8"))
if 'excerpt' in kwargs:
- values['excerpt'] = quote_plus(kwargs['excerpt'])
+ values['excerpt'] = quote_plus(kwargs['excerpt'].encode("utf-8"))
if 'source' in kwargs:
- values['source'] = quote_plus(kwargs['source'])
+ values['source'] = quote_plus(kwargs['source'].encode("utf-8"))
final_url = site_url % values
return final_url.replace('PERCENTSIGN', '%')

I'm not sure if this is something you want to add as I could a class method that returns the right string. Or maybe I missed something in the field type config..

Thanks for this app!

Post a comment