diff -r fa8873d3f1ab -r 03d79faa99fd docs/model-api.txt
a
|
b
|
|
2005 | 2005 | to display it, without repeating the URL information anywhere. You can still |
2006 | 2006 | use the ``get_absolute_url`` method in templates, as before. |
2007 | 2007 | |
| 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 | |
2008 | 2037 | Executing custom SQL |
2009 | 2038 | -------------------- |
2010 | 2039 | |