urlman

raw JSON →
2.0.3 verified Fri May 01 auth: no python

A Python library providing a declarative way to build URL patterns for Django models, similar to Django REST Framework's reverse patterns but more general. Current version 2.0.3, with a focus on simplicity and type safety. Release cadence is low; the library is in maintenance mode.

pip install urlman
error AttributeError: 'MyModel' object has no attribute 'urls'
cause The Urls class must be defined as a class attribute named 'urls' (lowercase) on the model.
fix
Define 'class urls(Urls):' inside your model, not 'class Urls(Urls):'.
error TypeError: 'Urls' object is not callable
cause Trying to call inst.urls() instead of accessing it as an attribute.
fix
Use inst.urls.detail, not inst.urls().
breaking In version 2.0, the import path changed: previously you could import from urlman.urls directly. Now all public API is from 'urlman'.
fix Update imports to 'from urlman import Urls'.
deprecated The 'name' argument when defining url patterns is deprecated; instead use explicit class methods or properties to generate named URLs.
fix Remove 'name' argument; use the method name as the URL name.
gotcha Do NOT use the @property decorator on methods within the Urls class. urlman handles access like attributes automatically.
fix Define methods that return strings; they are automatically treated as properties by urlman.

Define URL patterns as a nested Urls class on your Django model.

from django.db import models
from urlman import Urls

class MyModel(models.Model):
    name = models.CharField(max_length=100)

    class urls(Urls):
        detail = '/{self.pk}/'
        edit = '/{self.pk}/edit/'

inst = MyModel(pk=1, name='test')
print(inst.urls.detail)  # '/1/'
print(inst.urls.edit)    # '/1/edit/'