Skip to main content

Django Laconic URLS

https://travis-ci.org/robert-b-clarke/django-laconicurls.svg?branch=master Downloads

Introduction

django-laconicurls is different to other Django URL Shortener apps. It uses Django’s GenericForeignKeys and get_absolute_url to provide short urls for instances of Django models. The goal is to provide short, human readable urls, that can be featured within emails, print campaigns, qr codes and social media etc. These URLs will continue to work across site restructures etc

Quick start

  1. Run pip install django-laconicurls to install django-laconicurls

  2. Add “laconicurls” to INSTALLED_APPS in settings.py:

    INSTALLED_APPS = (
        ...
        'laconicurls',
    )
  3. Add laconicurls urlconf to urls.py. If possible it’s best to use a single case insensitive character for this:

    url(r'^(?i)Z', include('laconicurls.urls')),
  4. Run python manage.py migrate to create the necessary models.

  5. In your code, create some urls:

    from laconicurls.models import laconic_url_for_object
    #this can be any django object that supports get_absolute_url
    article = MyArticleModel.objects.get(pk=1)
    short_url = laconic_url_for_object(article)

Features

  • Creates and manages shortcut URLs for any instance of a Django model that implements get_absolute_url

  • Case insensitive Base27 encoding of URLs. Alphabet excludes vowels and numbers which look like vowels to reduce the likelyhood of offensive URLs (at least in English)

  • One laconic URL per object. Repeat calls to laconic_url_for_object for a given object will always result in the same URL

Example Usage

Say you have two models, Product and Promotion, which both implement get_absolute_url and have search engine friendly URLs:

>>> Product.objects.get(pk=100).get_absolute_url()
'/products/garden/100-fancy-green-electric-lawnmower'
>>> Promotion.objects.get(pk=50).get_absolute_url()
'/promotions-and-hot-deals/50-up-to-twenty-percent-off-gardening-equipment'

Clearly these URLs are not suitable for a print campaign, and are likely to change over time, subject to different SEO trends.

To add support for laconic URLs you need to pick a suitable prefix which doesn’t clash with any existing URLs. Ideally this will be a single character and won’t be case sensitive, but if you want it can be longer or contain slashes. For example, add the following to your urls.py to have laconicurls that begin with the letter Q:

url(r'^(?i)Q', include('laconicurls.urls')),

The easiest way to get laconic urls is to use the laconic_url_for_object helper. Example output might be as follows:

>>> from laconicurls.models import laconic_url_for_object
>>> laconic_url_for_object(Product.objects.get(pk=100))
'/QGH3'
>>> laconic_url_for_object(Promotion.objects.get(pk=50))
'/QGH4'

When accessed these URLs will redirect to the locations returned by their respectve get_absolute_url method calls

TODO

  • Add support for templatetags

  • Investigate ways to support alternate alphabets, as some users will not require case insensitive urls and may prefer a more efficient encoding (e.g. base 62)

  • Management commands for inspecting URLs

Metadata

Release files for django-laconicurls 0.1.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for django-laconicurls 0.1.1
File Size Uploaded
django-laconicurls-0.1.1.tar.gz 6.4 kB Details

Release files / django-laconicurls-0.1.1.tar.gz

Download URL django-laconicurls-0.1.1.tar.gz
Size 6.4 kB
Tags Source
SHA-256 checksum
How to use checksums
99294a92ea09942f9ca49d8e89fc637efd566c40d0326372587ad78464594a17
BLAKE2b-256 checksum
How to use checksums
e3d6741ddb4322e4822dfa2cffa59c131753fd41c6a89e4a58317f076a2d58a3
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Release history Release notifications | RSS feed

This release

0.1.1 This release

1 release file

0.1.0

1 release file

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page