Skip to main content

Django Simple URL Shortener

CI Python 3.12–3.15 Django 6.1 Bootstrap 5.3.9 License: GPL v2

A small, self-hosted URL shortener built as a reusable Django application. It creates compact base-62 links, supports optional user accounts, and keeps deployment under your control.

The package is published on PyPI.

Screenshots

The screenshots below are generated by the repository's Playwright script.

Home page Shortened URL
URL shortener home page Short URL creation result

Features

  • Shorten and permanently redirect to valid HTTP or HTTPS URLs.
  • Save links for signed-in users and browse them with pagination.
  • Generate compact, deterministic base-62 short codes by default, with a configurable encoder contract for custom code formats.
  • Use Django's built-in authentication, forms, templates, and ORM.
  • Responsive interface styled with locally bundled Bootstrap 5.3.9 assets.
  • Install the Django app independently of the included demo project.

Compatibility

Component Supported version
Python 3.12–3.15
Django 6.1.x
Database SQLite for local development; configure Django's database settings for deployment

The project is tested against all listed Python versions in CI. Django support follows the official Python compatibility table.

Quick start from source

git clone https://github.com/ricardodani/django-simple-url-shortner.git
cd django-simple-url-shortner
py -3.15 -m venv .venv
.\.venv\Scripts\Activate.ps1
python -m pip install --upgrade pip
python -m pip install -e .
python simple_url_shortner\manage.py migrate
python simple_url_shortner\manage.py runserver

Open http://127.0.0.1:8000/. On macOS or Linux, create and activate the virtual environment with python3.15 -m venv .venv and source .venv/bin/activate.

Add the app to a Django project

Install it from PyPI with:

python -m pip install django-simple-url-shortner

Register the app and its URL routes in your Django project:

# settings.py
INSTALLED_APPS = [
    # ...
    "urlshortener",
]
# urls.py
from django.urls import include, path

urlpatterns = [
    path("", include("urlshortener.urls")),
]

Run the app's migrations with python manage.py migrate. The app uses Django's configured user model and cache backend.

Configuration and deployment

The included demo project reads these environment variables:

Variable Purpose Default
DJANGO_DEBUG Enable Django debug mode true for local development
DJANGO_SECRET_KEY Secret key used when debug mode is disabled Development-only key when DJANGO_DEBUG=true
DJANGO_ALLOWED_HOSTS Comma-separated allowed host names localhost,127.0.0.1,testserver
DJANGO_DB_PATH SQLite database file path simple_url_shortner/db.sqlite3

For deployment, set DJANGO_DEBUG=false, provide a private DJANGO_SECRET_KEY, configure DJANGO_ALLOWED_HOSTS, and use a production database and cache appropriate to your workload. Never reuse the development settings as a production configuration. See Django's deployment checklist.

Short-code encoding

The default encoder converts each saved URL's integer database ID to base 62. Its alphabet is 0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ: 10 digits plus 26 lowercase and 26 uppercase letters. It repeatedly divides the ID by 62 and uses each remainder as a character, from right to left. For example, 61 becomes Z and 62 becomes 10.

This is reversible base conversion, not encryption: it uses no secret and does not hide or protect IDs. Short codes should not be treated as authorization tokens or as a way to keep URLs private.

To use another encoding, subclass UrlCodeEncoder and implement encode(value: int) -> str. The result must be a non-empty string of at most 20 ASCII letters or digits. Implementations should return stable, distinct codes for distinct IDs. Configure the dotted Python path to the class in the Django project settings:

# myproject/url_codes.py
from urlshortener.converter import UrlCodeEncoder


class HexUrlCodeEncoder(UrlCodeEncoder):
    def encode(self, value: int) -> str:
        if value < 0:
            raise ValueError("value must be non-negative")
        return f"x{value:x}"
# settings.py
URLSHORTENER_CODE_ENCODER = "myproject.url_codes.HexUrlCodeEncoder"

The encoder is instantiated without arguments when a new URL is saved. Existing short codes are retained when their URL records are updated.

Development

Install the development tools and Playwright:

python -m pip install -e ".[dev]"
python -m playwright install chromium

Run the unit and Django integration tests:

coverage run -m pytest
coverage report

Run lint, formatting checks, and pre-commit hooks:

ruff check .
ruff format --check .
mypy
pre-commit run --all-files

Run the Playwright end-to-end smoke test and refresh the checked-in screenshots:

python scripts/capture_screenshots.py

The script starts an isolated demo database and local Django server, exercises URL creation and redirection in Chromium, then writes PNGs to docs/screenshots/. Pass --output-dir PATH to write the screenshots to another directory inside the repository or --port PORT to use a different local port. For safety, output paths outside the repository are rejected.

Build and validate the distribution without publishing it:

python -m build --sdist --wheel
python -m twine check dist/*

AI and MCP integrations

AI integrations are not part of the core package. If an MCP interface is added, it should be a separate, optional adapter over the same URL-shortening operations. The adapter can use the official MCP Python SDK; Agno can consume that MCP server when an Agno-based client is useful, without making Agno a required dependency of this Django application.

Contributing

Bug reports, focused improvements, and tests are welcome. Please include the problem being solved, steps to reproduce when relevant, and the checks you ran. Pull requests require maintainer review and manual approval before merge.

License

This project is distributed under the GNU General Public License, version 2. See LICENSE.

Metadata

Release files for django-simple-url-shortner 0.2.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-simple-url-shortner 0.2.1
File Size Uploaded Provenance Actions
django_simple_url_shortner-0.2.1.tar.gz 78.2 kB Without provenance Details for django_simple_url_shortner-0.2.1.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for django-simple-url-shortner 0.2.1
File Interpreter ABI Platform Actions
django_simple_url_shortner-0.2.1-py3-none-any.whl Python 3 none any Details for django_simple_url_shortner-0.2.1-py3-none-any.whl

Total release size: 157.7 kB

Release files / django_simple_url_shortner-0.2.1.tar.gz

Download URL django_simple_url_shortner-0.2.1.tar.gz
Size 78.2 kB
Tags Source
SHA-256 checksum
How to use checksums
f3721310e52f4c66dd73170941a05290a22dea01be11202f53a2ccd094bfd5f2
BLAKE2b-256 checksum
How to use checksums
3866972ecbea5293886b7ac043eacd9e8a953356817a156d219faad5be1a2b27
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.12.10

Release files / django_simple_url_shortner-0.2.1-py3-none-any.whl

Download URL django_simple_url_shortner-0.2.1-py3-none-any.whl
Size 79.5 kB
Tags Python 3
SHA-256 checksum
How to use checksums
ccb7ed20b5dc4ab5acd45fe9e01a65fdfe175a3da30453d5d74f1d21d8d64919
BLAKE2b-256 checksum
How to use checksums
778acd86fc7eaf7b4a7a584888ee3f4250f482aaa4003e3ed8a8325eb799cc3c
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.12.10

Security for django-simple-url-shortner 0.2.1

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.

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