Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/andrewgodwin/urlman
https://github.com/andrewgodwin/urlman
Last synced: 5 days ago
JSON representation
- Host: GitHub
- URL: https://github.com/andrewgodwin/urlman
- Owner: andrewgodwin
- License: apache-2.0
- Created: 2015-08-25T07:18:54.000Z (about 9 years ago)
- Default Branch: master
- Last Pushed: 2024-05-21T14:56:57.000Z (6 months ago)
- Last Synced: 2024-09-20T10:08:45.494Z (about 1 month ago)
- Language: Python
- Size: 35.2 KB
- Stars: 117
- Watchers: 9
- Forks: 8
- Open Issues: 0
-
Metadata Files:
- Readme: README.rst
- Changelog: CHANGELOG
- License: LICENSE
Awesome Lists containing this project
- -awesome-django - urlman - A nicer way to do URLs for Django models. (Third-Party Packages / URLs)
- awesome-django - urlman - A nicer way to do URLs for Django models. (Third-Party Packages / URLs)
- starred-awesome - urlman - (Python)
README
urlman
------.. image:: https://travis-ci.org/andrewgodwin/urlman.svg?branch=master
:target: https://travis-ci.org/andrewgodwin/urlman
:alt: Test Status.. image:: https://codecov.io/gh/andrewgodwin/urlman/branch/master/graph/badge.svg
:target: https://codecov.io/gh/andrewgodwin/urlman
:alt: Test Coverage StatusA 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:
.. code-block:: python
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:.. code-block:: python
def get_absolute_url(self):
return self.urls.viewTo build a full URL use the ``full`` method like this:
.. code-block:: python
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):.. code-block:: python
from urlman.serializers import UrlManField
class MySerializer(ModelSerializer):
urls = UrlManField(urls=['view', 'edit'], attribute='urls', full=True)