[Answer]-Capturing objects in Django Url

1👍

Use named groups.

It’s possible to use named regular-expression groups to capture URL
bits and pass them as keyword arguments to a view.

urls.py:

from django.conf.urls import patterns, url

urlpatterns = patterns('',
    url(r'^(?P<name>\w+)/$', 'my_view'),)

views.py:

def my_view(request, name=None):
    # get a model instance
    animal = animals.objects.get(name=name)

Hope that helps.

👤alecxe

Leave a comment