Skip to main content

django-url-shortening

A custom URL shortening app for Django with API.

Usage

  1. Add urlshortening app to your INSTALLED_APPS and do migrate

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

    ('^linkshortening/', include('urlshortening.urls'))
  3. Add settings (more about parameters further)

    INITIAL_URL_LEN = 6
    RETRY_COUNT = 5
    SHORT_URL_PATH = 'http://example.com/short-prefix/'
    REDIRECT_PREFIX = 'r'
  4. Now you can use API to make short links

    POST linkshortening/short/

    With json data {"full_url": "http://example.com/a/b/c/d/e"}

    And get response

    {"data": {
        "short_id": "123456",
        "short_url_path": "http://example.com/short-prefix/"
     }, "error": ""}
  5. You could also use urlshortening right from code

    from urlshortening.models import get_short_url, invalidate_url, get_full_url
    url = "http://example.com/a/b/c/d/e"
    short_url = get_short_url(url) # Url object
    print(short_url.short_id) # id for short url
    full_url = get_full_url(short_url.short_id) # Url object
    print(full_url.url) # "http://example.com/a/b/c/d/e"
    # You could also invalidate url
    invalidate_url(full_url.short_id)

API

Get short link

  • URL

    /short/
  • Method: POST

  • Data Params

    • full_url

  • Success Response:

    • Code: 200 Content: { data: { "short_url_path": "000001" }, error: "" }

  • Error Response:

    • Code: 400 Content: { error : "full_url is empty" }

    • Code: 400 Content: { error : "full_url is too long" }

Get full link

  • URL

    /expand/:short_id/
  • Method: GET

  • URL Params

    short_id=[string]
  • Success Response:

    • Code: 200 Content: { error : "", data: { full_url: "http://example.com/to-000001" }}

  • Error Response:

    • Code: 404 Content: { error : "Link is expired" }

    • Code: 404 Content: { error : "Url doesn\'t exist" }

Get redirect

  • URL

    /REDIRECT_PREFIX/expand/:short_id/
  • Method: GET

  • URL Params

    short_id=[string]
  • Success Response:

    • Code: 302

  • Error Response:

    • Code: 404 Content: { error : "Link is expired" }

    • Code: 404 Content: { error : "Url doesn\'t exist" }

Invalidate url

  • URL

    /invalidate/
  • Method: POST

  • Data Params

    • short_id

  • Success Response:

    • Code: 200 Content: { error : "", data: { "short_id": "000001", "invalidated": "true" } }

  • Error Response:

    • Code: 400 Content: { error : "short_id is empty" }

    • Code: 400 Content: { error : "Link is already expired" }

    • Code: 404 Content: { error : "Url doesn\'t exist" }

Settings

Available settings are:

  • INITIAL_URL_LEN

    Initial length of short id for url. Once you get more short id’s than is possible in all combinations of INITIAL_URL_LEN symbols it will increase by one

  • RETRY_COUNT

    How many times do we to check before increasing INITIAL_URL_LEN.

  • SHORT_URL_PATH

    Url that will be returned with short_id on POST linkshortening/short/ request. It might help you to construct full url.

  • REDIRECT_PREFIX

    Select prefix to use redirect links. For example REDIRECT_PREFIX="r" and we get redirect links with format /r/expand/{short_id}/

Metadata

Release files for django-url-shortening 1.0.0

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-url-shortening 1.0.0
File Size Uploaded Provenance Actions
django-url-shortening-1.0.0.tar.gz 5.6 kB Without provenance Details for django-url-shortening-1.0.0.tar.gz

Release files / django-url-shortening-1.0.0.tar.gz

Download URL django-url-shortening-1.0.0.tar.gz
Size 5.6 kB
Tags Source
SHA-256 checksum
How to use checksums
5d94c6adc46dd80427e73b1c7433dbd8af240037c9960f778d1fedb6489e9f4e
BLAKE2b-256 checksum
How to use checksums
cb88281ed0e22547a94a6042a53eb639e2b637e6df95f5cfafda8e25d67174a2
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Security for django-url-shortening 1.0.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

1.0.0 This release

1 release file No provenance

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