Django URL pattern helpers
Project description
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)
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file urlman-2.0.2.tar.gz
.
File metadata
- Download URL: urlman-2.0.2.tar.gz
- Upload date:
- Size: 7.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.11.2
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 231afe89d0d0db358fe7a2626eb39310e5bf5911f3796318955cbe77e1b39601 |
|
MD5 | 7918492cc6459ace74179c894a95fff9 |
|
BLAKE2b-256 | 65c3cc163cadf40a03d23d522d050ffa147c0589ccd7992a2cc4dd2b02aa9886 |
File details
Details for the file urlman-2.0.2-py2.py3-none-any.whl
.
File metadata
- Download URL: urlman-2.0.2-py2.py3-none-any.whl
- Upload date:
- Size: 8.0 kB
- Tags: Python 2, Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.11.2
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 2505bf310be424ffa6f4965a6f643ce32dc6194f61a3c5989f2f56453c614814 |
|
MD5 | 88bb026bf6fc3fb473b968a83dfd19e7 |
|
BLAKE2b-256 | f40ce8a418c9bc9349e7869e88a5b439cf39c4f6f8942da858000944c94a8f01 |