Skip to main content

A URL shortening app for Django.

Project description

django-shorturls

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

django_shorturls-3.0.0.tar.gz (8.5 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

django_shorturls-3.0.0-py3-none-any.whl (11.4 kB view details)

Uploaded Python 3

File details

Details for the file django_shorturls-3.0.0.tar.gz.

File metadata

  • Download URL: django_shorturls-3.0.0.tar.gz
  • Upload date:
  • Size: 8.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.10.4 {"installer":{"name":"uv","version":"0.10.4","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"macOS","version":null,"id":null,"libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

File hashes

Hashes for django_shorturls-3.0.0.tar.gz
Algorithm Hash digest
SHA256 6117da6b9f0442984b4ec02fa320b6c4cb8da71b447c6a29c790ca5b663476ef
MD5 4f7f717d4fc530e06111fd1287ad3d98
BLAKE2b-256 afe360a24b4547a2f508a9863bf02cab6dfccdcd6f26ca95c57a8daf64d86e03

See more details on using hashes here.

File details

Details for the file django_shorturls-3.0.0-py3-none-any.whl.

File metadata

  • Download URL: django_shorturls-3.0.0-py3-none-any.whl
  • Upload date:
  • Size: 11.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.10.4 {"installer":{"name":"uv","version":"0.10.4","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"macOS","version":null,"id":null,"libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

File hashes

Hashes for django_shorturls-3.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 615548e1d303e40ef414cf828ec3ced464fd96440e86c8a0083c1d57999aa9a1
MD5 154772ad31cff52ce1ed7e17ead9bfa1
BLAKE2b-256 9be2c20626fc167920d15ab4e4bd7d68e5560d901badea9959d1bde43b514942

See more details on using hashes here.

Supported by

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