Django

Code

Changeset 4879

Show
Ignore:
Timestamp:
03/31/07 06:35:06 (2 years ago)
Author:
mtredinnick
Message:

Fixed #3540 -- Updated permalink documentation to fix an error and document how
to pass keyword arguments.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • django/trunk/docs/model-api.txt

    r4804 r4879  
    17591759 
    17601760You can further decouple your models from the URLconf using the ``permalink`` 
    1761 decorator. This decorator is passed the view function and any parameters you 
    1762 would use for accessing this instance directly. Django then works out the 
    1763 correct full URL path using the URLconf. For example:: 
     1761decorator. This decorator is passed the view function, a list of positional 
     1762parameters and (optionally) a dictionary of named parameters.  Django then 
     1763works out the correct full URL path using the URLconf, substituting the 
     1764parameters you have given into the URL. For example, if your URLconf 
     1765contained a line such as:: 
     1766 
     1767    (r'^/people/(\d+)/$', 'people.views.details'), 
     1768 
     1769...your model could have a ``get_absolute_url`` method that looked like this:: 
    17641770 
    17651771    from django.db.models import permalink 
    17661772 
    17671773    def get_absolute_url(self): 
    1768         return ('people.views.details', str(self.id)
     1774        return ('people.views.details', [str(self.id)]
    17691775    get_absolute_url = permalink(get_absolute_url) 
     1776 
     1777 
     1778Similraly, if you had a URLconf entry that looked like:: 
     1779 
     1780    (r'/archive/(?P<year>\d{4})/(?P<month>\d{1,2})/(?P<day>\d{1,2})/$', 
     1781        archive_view) 
     1782 
     1783...you could reference this using ``permalink()`` as follows:: 
     1784 
     1785    def get_absolute_url(self): 
     1786        return ('archive_view', (), { 
     1787            'year': self.created.year, 
     1788            'month': self.created.month, 
     1789            'day': self.created.day}) 
     1790    get_absolute_url = permalink(get_absolute_url) 
     1791 
     1792Notice that we specify an empty sequence for the second argument in this case, 
     1793since we're only wanting to pass in some keyword arguments. 
    17701794 
    17711795In this way, you're tying the model's absolute URL to the view that is used