georoot georoot - 1 year ago 172
Python Question

URL shortening in django

I want to write a small django app that will shorten all the internal urls of the project. Till now i have got short_url which is pretty much what we want. Except it accepts only an int. Now there are a couple of other parameters also that we need to encode in the url. Need to know how can that be done.

Initially we though of hash based implementation, but we would need to store that in our databases so that is no good.

Encrypting data with key was also an option but the output is too long for short url.

Can someone suggest a library or algorithm that can be used for the same. unlike short_url, we have ascii data also. And as far as possible we would like the application to be stateless (no storage in DB) or if inevetable at-least not check DB every time if the url has been generated (low / no collision)


For an example currently we have url format as /project/application .application is a list that is associated with the project. Now what we need is a bijective function that takes in project+application and gives out short url. (/Gjd73 for example). project name is defined by out users and application name is predefined in our database.

there can be any number of projects and application, both of them are ascii, but because there pk value can also be used in case its more useful than the ascii name.

Answer Source

If in your database, every "application" is ForeignKey-related to a parent "project", you can drop the "project" name/Id from you URL path.

A path like (where "12345" is your application pk) is about as short as it gets.

You can even make it shorter and Base64 encode that number, so you get an even shorter alphanum value.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download