Skip to main content

A URL shortening app for Django (branched version maintained by Airsupply).

Project description

django-shorturls

https://travis-ci.org/bfirsh/django-shorturls.svg?branch=master https://badge.fury.io/py/django-shorturls.svg

A custom URL shortening app for Django, including easy rev=canonical support.

Most code was originally by Simon Willison; see https://simonwillison.net/2009/Apr/11/revcanonical/ for details. Improved slightly and packaged by Jacob Kaplan-Moss. Currently maintained by Ben Firshman.

Patches welcome: http://github.com/bfirsh/django-shorturls

Usage

So, you want to host your own short URLs on your Django site:

  1. In your settings, define a set of prefixes for short URLs:

    SHORTEN_MODELS = {
        'A': 'myapp.animal',
        'V': 'myapp.vegetable',
        'M': 'myapp.mineral'
    }

    The keys are string prefixes; they can be any string, actually, but since we’re going for short a single character is probably good.

    Values are the (hopefully-familiar) "<app-name>.<model-class>" used by Django to identify a model. Remember: app-name is the (case-sensitive) last bit of your app’s name in INSTALLED_APPS, and <model-class> is your model class’s name, lowercased.

    Make sure your models have a get_absolute_url() method defined.

  2. Wire up the redirect view by adding to your URLconf:

    ('^short/', include('shorturls.urls'))
  3. If you’d like to quickly link to shortened URLs in your templates, stick "shorturls" in INSTALLED_APPS, and then in your templates do:

    {% load shorturl %}
    <a href="{% shorturl object %}">...</a>

    (where object is a model instance).

    Alternatively:

    {% load shorturl %}
    {% revcanonical object %}

    This generates the whole <link rev="canonical" href="..."> tag for you.

That’s it.

If you’d like more control, keep reading.

Settings

Available settings are:

SHORTEN_MODELS

You’ve seen this one.

SHORT_BASE_URL

If defined, the shorturl and revcanonical template tags will prefix generated URLs with this value. Use this if you’ve got a shorter domain name you’d like to use for small URLs.

For example, given SHORT_BASE_URL = 'http://exm.pl/', {% shorturl obj %} would return something like http://exm.pl/AbCd.

SHORTEN_FULL_BASE_URL

The domain to redirect to when redirecting away from the small URL. Again, you’ll use this if your short URL base and your “real” site differ.

If not defined, the redirect view will try to guess the proper domain by consulting the django.contrib.sites framework, if installed, or the requested domain, if not.

SHORTURLS_DEFAULT_CONVERTER

The converter that is used to translate between short URLs and model IDs. Defaults to the built in base 62 conversion.

Available converters:

  • shorturls.baseconv.base62 Base 62 encoding.

  • shorturls.baseconv.base32 Douglas Crockford’s base 32.

  • shorturls.baseconv.hexconv Hex encoding.

  • shorturls.baseconv.bin Binary encoding, because why not.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

airsupply-django-shorturls-2.0.1.tar.gz (8.8 kB view details)

Uploaded Source

File details

Details for the file airsupply-django-shorturls-2.0.1.tar.gz.

File metadata

  • Download URL: airsupply-django-shorturls-2.0.1.tar.gz
  • Upload date:
  • Size: 8.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.22.0 setuptools/42.0.2 requests-toolbelt/0.9.1 tqdm/4.41.0 CPython/3.7.3

File hashes

Hashes for airsupply-django-shorturls-2.0.1.tar.gz
Algorithm Hash digest
SHA256 a5495e1de6a56b340f40f28b1b7b433fe8866027a36e5165d55ffe0953f66db0
MD5 5cea01a2c71659dd325be364f668fe42
BLAKE2b-256 d05369a15346be80a2f2254a22ff6ded11fbf17ac6b9e3eec84400399ea7f585

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page