| | 2008 | In some cases, such as the use of generic views or the re-use of custom views for multiple models, specifying the view function may confuse the reverse URL matcher (because multiple patterns point to the same view). |
|---|
| | 2009 | |
|---|
| | 2010 | For that problem, Django has **named URL patterns**, it's possible to give a name to a pattern (by replacing the pattern tuple by a call to the ``url`` function):: |
|---|
| | 2011 | |
|---|
| | 2012 | from django.conf.urls.defaults import * |
|---|
| | 2013 | |
|---|
| | 2014 | url(r'^people/(\d+)/$', 'django.views.generic.list_detail.object_detail', name='people_view'), |
|---|
| | 2015 | |
|---|
| | 2016 | and then use that name to perform the reverse URL resolution instead of the view name:: |
|---|
| | 2017 | |
|---|
| | 2018 | from django.db.models import permalink |
|---|
| | 2019 | |
|---|
| | 2020 | def get_absolute_url(self): |
|---|
| | 2021 | return ('people_view', [str(self.id)]) |
|---|
| | 2022 | get_absolute_url = permalink(get_absolute_url) |
|---|
| | 2023 | |
|---|
| | 2024 | More complete informations on named URL patterns are available at `url dispatch`_. |
|---|
| | 2025 | |
|---|
| | 2026 | .. _url dispatch: ../url_dispatch/#naming-url-patterns |
|---|
| | 2027 | |
|---|