CARVIEW |
Select Language
HTTP/2 200
server: nginx
content-type: text/html;charset=utf-8
cache-control: must-revalidate
expires: Fri, 01 Jan 1999 00:00:00 GMT
set-cookie: trac_form_token=f877c7fe8d67611b83198108; HttpOnly; Path=/; Secure
set-cookie: trac_session=c800c078cf0ab16f1ae0445f; expires=Tue, 21 Oct 2025 05:39:29 GMT; HttpOnly; Path=/; Secure
strict-transport-security: max-age=31536000; includeSubDomains; preload
permissions-policy: interest-cohort=()
x-content-type-options: nosniff
x-frame-options: SAMEORIGIN
x-xss-protection: 1; mode=block
accept-ranges: bytes
via: 1.1 varnish, 1.1 varnish
date: Wed, 23 Jul 2025 05:39:29 GMT
x-served-by: cache-fra-eddf8230021-FRA, cache-bom-vanm7210069-BOM
x-cache: MISS, MISS
x-cache-hits: 0, 0
x-timer: S1753249169.308680,VS0,VE267
vary: Accept-Encoding
GenerateGenericURLs – Django
Back to Top
Django
The web framework for perfectionists with deadlines.
Issues
Generate Generic URLs
Here's a little function I wrote to generate a standard set of URL mappings I use with generic views. It takes a list of tuples that each contain the name to use for the url and the model class that represents it. This function generates generic views for listing, viewing details, adding, editing and deleting objects for the given model. For example:
make_url_list([('widget', Widget)])
will create:
^/widget/$ # will list all widgets ^/widget/(?<object_id>\d+)/$ # will give details for a specific object ^/widget/edit/(?<object_id>\d+)/$ # will edit an object ^/widget/delete/(?<object_id>\d+)/$ # will delete an object ^/widget/add/$ # will add an object
Here's the function:
def make_url_list(input, prefix=''): l = [] for (name, model) in input: l.append((r'^%s/$' % name, 'django.views.generic.list_detail.object_list', dict(queryset=model.objects.all(), allow_empty=True))) l.append( (r'^%s/(?P<object_id>\d+)/$' %name, 'django.views.generic.list_detail.object_detail', dict(queryset=model.objects.all()))) l.append( (r'^%s/add/$' % name, 'django.views.generic.create_update.create_object', dict(model=model, post_save_redirect='%s/%s/%s/' % (prefix, name, '%(id)s')))) l.append( (r'^%s/edit/(?P<object_id>\d+)/$' % name, 'django.views.generic.create_update.update_object', dict(model=model, post_save_redirect='%s/%s/%s/' % (prefix, name, '%(id)s')))) l.append( (r'^%s/delete/(?P<object_id>\d+)/$' % name, 'django.views.generic.create_update.delete_object', dict(model=model, post_delete_redirect='%s/%s/' % (prefix, name)))) return l
This function could be generalized a bit more, but I've found it handy as is. I use it in my urls.py like this:
tup = tuple(make_url_list(input,prefix='/prefix_if_needed')) urlpatterns = patterns('', (r'^someotherurl/$', 'some.other.package'), *tup)
Last modified
19 years ago
Last modified on Aug 16, 2006, 7:42:30 PM
Note:
See TracWiki
for help on using the wiki.
Download in other formats:
Django Links
Learn More
Get Involved
Follow Us
- Hosting by In-kind donors
- Design by Threespot &
© 2005-2025 Django SoftwareFoundation unless otherwise noted. Django is a registered trademark of the Django Software Foundation.