Source

django-shorty /

Filename Size Date modified Message
shorty
58 B
1.5 KB
3.4 KB
161 B
478 B

Django-Shorty

Django shorty makes it real easy to create short urls from longer urls in your application. Works for both external and internal URLS

Requirements

Django 1.1 or higher Django South 0.7 or higher

Setup

Add shorty to your installed apps in your settings.py

Add the shorty.urls to your projects urls.py

Add the following variables to your settings.py

SHORT_URL_DOMAIN (required)

This is the URL for your short url service. No Trailing slash

# SHORT_URL_DOMAIN = "http://example.com/s"

SHORTY_UNIQUE_CODE (required)

This a unique code that you would use to make it harder for someone to guess your short URL sequence, it randomized the code values that are returned.

# base62_digits = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ" # l = list(base62_digits) # import random # random.seed(54) # replace 54 with a random number # random.shuffle(l) # s = ''.join(l) # print s # ###############################################################################################

# SHORTY_UNIQUE_CODE = 'eK1BPRYNwCVjhcfuZ4OSIFtzGiL63mry790dXgDaH5bEnoWqQ2AJMkvplTsx8U'

SHORTY_PRE_SEED (optional)

DON'T CHANGE ONCE YOU HAVE STARTED USING, OR ELSE YOU COULD END UP WITH DUPLICATES!

If you want your encoded value to always start with a value use this.

# SHORTY_PRE_SEED = "A" # all encoded values would look like this. http://example.com/s/A{encoded_value}

SHORTY_POST_SEED (optional)

DON'T CHANGE ONCE YOU HAVE STARTED USING, OR ELSE YOU COULD END UP WITH DUPLICATES!

If you want your encoded value to always end with a value use this.

# SHORTY_POST_SEED = "Z" # all encoded values would look like this. http://example.com/s/{encoded_value}Z

SHORTY_HOME_URL_REDIRECT (required)

Redirect to the website defined in SHORTY_HOME_URL_REDIRECT. This is setup so that you can have a website that just does redirects and another site with all content. This will redirect home page to the other site.

# SHORTY_HOME_URL_REDIRECT = "http://example.com"

How to Use

Anywhere you have a long url you can call the get_shorty method and it will return a short version of it.

from shorty import get_shorty

# the long url needs to be a fully qualified URL, no relative urls. my_long_url = "http://kencochrane.net/blog/2011/06/django-gunicorn-nginx-supervisord-fabric-centos55/"

short_url = get_shorty(my_long_url)

print short_url

or if you have a model you can add a property to that model and get a short url real easy.

Property def short_url(self):

""" If your model has an absolute_url method that returns the absolute_url for itself
then you can use that to get the url and then call get_shorty """

from shorty import get_shorty return get_shorty(self.absolute_url)

Property def short_url2(self):

""" Use the reverse utility method to generate a url
and then make it short and return it """

from django.core.urlresolvers import reverse from shorty import get_shorty

url = reverse('view_post', args=[self.pk]) return get_shorty(self.absolute_url)