• Stars
    star
    115
  • Rank 305,916 (Top 7 %)
  • Language
    Python
  • License
    Apache License 2.0
  • Created about 9 years ago
  • Updated almost 2 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

urlman

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)

More Repositories

1

channels-examples

Example projects using Django Channels
Python
1,218
star
2

lastgraph

Archived code for LastGraph
Python
151
star
3

django-hatchway

An unsurprising Django API framework
Python
137
star
4

lidartile

Makes 3D printable terrain tiles from LIDAR data
Python
120
star
5

weatherboard

Source for my e-paper based weather display.
Python
72
star
6

yamdl

ORM-queryable YAML fixtures for Django
Python
65
star
7

twintubes

The code behind A Series Of Twin Tubes
Python
47
star
8

south2

South 2: Backported django.db.migrations for older Django versions
Python
43
star
9

stations

The source code behind stations.aeracode.org
JavaScript
40
star
10

arduino-examples

Example code for Arduino and other embedded devices that use its IDE
C++
36
star
11

routerdash

A prototype UI for my custom router
CSS
28
star
12

rfid-inventory

An RFID inventory system
Python
26
star
13

asgigram

ASGI/Telegram protocol server
Python
26
star
14

dockerfiles

Docker files used for deploying my server components
Python
21
star
15

uk-usa-dictionary

A UK-USA dictionary of common terms, phrases and things
21
star
16

migrations

An unstable, work-in-progress candidate for django.contrib.migrations
Python
18
star
17

landcarve

GIS scripts and tools
Python
16
star
18

grorg

JavaScript
11
star
19

frequensgi

ASGI APRS-IS protocol server
Python
9
star
20

django-stator

A state machine-based background worker system for Django
Python
7
star
21

arcdiscvist

Archival/Backup media manager and indexer
Python
4
star
22

thiuff

Python
4
star
23

oxstu

Oxford Student
Python
3
star
24

plex-tools

Random Plex tooling to help with organisation
Python
3
star
25

kugelblitz

An experiment to compile a JavaScript subset to Python
Python
3
star
26

airport-mogul

An attempt to build an Airport Tycoon clone. Incredibly basic code so far.
C
3
star
27

datastore-workshop

Example code from "Datastores, Python and You" workshop
Python
2
star
28

vfrmap

Code for my live, LED-based aviation weather map
C++
2
star
29

twoup

SVG-based presentation tools
Python
1
star
30

todoscreens

Python
1
star
31

django-singlefile

Python
1
star