Skip to content

Instantly share code, notes, and snippets.

@rafaelcanovas
Created October 17, 2013 17:32
Show Gist options
  • Save rafaelcanovas/7028987 to your computer and use it in GitHub Desktop.
Save rafaelcanovas/7028987 to your computer and use it in GitHub Desktop.
bit.ly templatetag for Django
# coding: utf-8
import bitly_api
from django import template
from django.conf import settings
from django.core.exceptions import ImproperlyConfigured
register = template.Library()
try:
BITLY_LOGIN = settings.BITLY_LOGIN
BITLY_API_KEY = settings.BITLY_API_KEY
except AttributeError:
raise ImproperlyConfigured('Missing bitly login or api key')
@register.filter
def bitly_shorten(url):
bitly = bitly_api.Connection(BITLY_LOGIN, BITLY_API_KEY)
try:
return bitly.shorten(url)['url']
except:
return ''
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment