Django

Code

Ticket #7216: permalink_doc.diff

File permalink_doc.diff, 1.3 kB (added by masklinn, 5 months ago)
  • a/docs/model-api.txt

    old new  
    20052005to display it, without repeating the URL information anywhere. You can still 
    20062006use the ``get_absolute_url`` method in templates, as before. 
    20072007 
     2008In 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 
     2010For 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 
     2016and 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 
     2024More complete informations on named URL patterns are available at `url dispatch`_. 
     2025 
     2026.. _url dispatch: ../url_dispatch/#naming-url-patterns 
     2027 
    20082028Executing custom SQL 
    20092029-------------------- 
    20102030