Skip to main content

Django URL pattern helpers

Project description

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

model 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.

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

urlman-1.2.0.tar.gz (3.6 kB view hashes)

Uploaded Source

Built Distribution

urlman-1.2.0-py2-none-any.whl (5.4 kB view hashes)

Uploaded Python 2

Supported by

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