Skip to main content
Test Status Test Coverage Status

A nicer way to do URLs for Django models.

Replaces things like get_absolute_url with a .urls attribute that can reference other URLs and build sensible trees of things, and can then be accessed using instance.urls.name.

This is so you can have URLs on your model instances directly (rather than reversing through the url lookup functions, which is not only slow but often hard to supply arguments to). You can just throw {{ instance.urls.view }} into a template to get a link.

It also lets you use Python string formatting syntax to place arguments into URLs from the model instance itself or from other URLs in the same set.

Example:

import urlman

class Group(models.Model):

...

    class urls(urlman.Urls):
        view = "/{self.slug}/"
        users = "{view}users/"
        admin = "{view}admin/"


def my_view(request):
    group = ...
    return redirect(group.urls.view)

It’s suggested that you use “view” as the equivalent name for get_absolute_url, and have a function like this on your model:

def get_absolute_url(self):
    return self.urls.view

To build a full URL use the full method like this:

def my_view(request):
    group = ...
    return redirect(group.urls.admin.full(scheme='https'))

You can implement the get_scheme(url) and get_hostname(url) methods on your Url class to change your default theme and hostname from the urlman defaults of ‘http’ and ‘localhost’, respectively.

If you use Django REST Framework, you can use urlman.UrlManField to provide an object with a set of URLs. It is used like this (only the urls parameter is required):

from urlman.serializers import UrlManField

class MySerializer(ModelSerializer):
    urls = UrlManField(urls=['view', 'edit'], attribute='urls', full=True)

Release files for urlman 2.0.2

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

Source distribution (sdist)

Source distribution for urlman 2.0.2
File Size Uploaded
urlman-2.0.2.tar.gz 7.7 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for urlman 2.0.2
File Interpreter ABI Platform
urlman-2.0.2-py2.py3-none-any.whl Python 3, Python 2 none any Details

Total release size: 15.7 kB

Release files / urlman-2.0.2.tar.gz

Download URL urlman-2.0.2.tar.gz
Size 7.7 kB
Tags Source
SHA-256 checksum
How to use checksums
231afe89d0d0db358fe7a2626eb39310e5bf5911f3796318955cbe77e1b39601
BLAKE2b-256 checksum
How to use checksums
65c3cc163cadf40a03d23d522d050ffa147c0589ccd7992a2cc4dd2b02aa9886
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.11.2

Release files / urlman-2.0.2-py2.py3-none-any.whl

Download URL urlman-2.0.2-py2.py3-none-any.whl
Size 8.0 kB
Tags Python 2 Python 3
SHA-256 checksum
How to use checksums
2505bf310be424ffa6f4965a6f643ce32dc6194f61a3c5989f2f56453c614814
BLAKE2b-256 checksum
How to use checksums
f40ce8a418c9bc9349e7869e88a5b439cf39c4f6f8942da858000944c94a8f01
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.11.2

Release history Release notifications | RSS feed

2.0.3

2 release files

This release

2.0.2 This release

2 release files

2.0.1

2 release files

2.0.0

2 release files

1.4.0

2 release files

1.3.0

2 release files

1.2.0

2 release files

1.1.0

2 release files

1.0.1

1 release file

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