Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Fixed #3540 -- Updated permalink documentation to fix an error and do…

…cument how

to pass keyword arguments.


git-svn-id: http://code.djangoproject.com/svn/django/trunk@4879 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit d7cf8eb406bd3e7c6cf2c09a103935b219b261e5 1 parent 6a5deb6
Malcolm Tredinnick authored March 31, 2007

Showing 1 changed file with 28 additions and 4 deletions. Show diff stats Hide diff stats

  1. 32  docs/model-api.txt
32  docs/model-api.txt
@@ -1758,16 +1758,40 @@ slightly violates the DRY principle: the URL for this object is defined both
1758 1758
 in the URLConf file and in the model.
1759 1759
 
1760 1760
 You 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::
  1761
+decorator. This decorator is passed the view function, a list of positional
  1762
+parameters and (optionally) a dictionary of named parameters.  Django then
  1763
+works out the correct full URL path using the URLconf, substituting the
  1764
+parameters you have given into the URL. For example, if your URLconf
  1765
+contained 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::
1764 1770
 
1765 1771
     from django.db.models import permalink
1766 1772
 
1767 1773
     def get_absolute_url(self):
1768  
-        return ('people.views.details', str(self.id))
  1774
+        return ('people.views.details', [str(self.id)])
  1775
+    get_absolute_url = permalink(get_absolute_url)
  1776
+
  1777
+
  1778
+Similraly, 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})
1769 1790
     get_absolute_url = permalink(get_absolute_url)
1770 1791
 
  1792
+Notice that we specify an empty sequence for the second argument in this case,
  1793
+since we're only wanting to pass in some keyword arguments.
  1794
+
1771 1795
 In this way, you're tying the model's absolute URL to the view that is used
1772 1796
 to display it, without repeating the URL information anywhere. You can still
1773 1797
 use the ``get_absolute_url`` method in templates, as before.

0 notes on commit d7cf8eb

Please sign in to comment.
Something went wrong with that request. Please try again.