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