Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

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
@malcolmt malcolmt authored
Showing with 28 additions and 4 deletions.
  1. +28 −4 docs/model-api.txt
View
32 docs/model-api.txt
@@ -1758,16 +1758,40 @@ slightly violates the DRY principle: the URL for this object is defined both
in the URLConf file and in the model.
You can further decouple your models from the URLconf using the ``permalink``
-decorator. This decorator is passed the view function and any parameters you
-would use for accessing this instance directly. Django then works out the
-correct full URL path using the URLconf. For example::
+decorator. This decorator is passed the view function, a list of positional
+parameters and (optionally) a dictionary of named parameters. Django then
+works out the correct full URL path using the URLconf, substituting the
+parameters you have given into the URL. For example, if your URLconf
+contained a line such as::
+
+ (r'^/people/(\d+)/$', 'people.views.details'),
+
+...your model could have a ``get_absolute_url`` method that looked like this::
from django.db.models import permalink
def get_absolute_url(self):
- return ('people.views.details', str(self.id))
+ return ('people.views.details', [str(self.id)])
+ get_absolute_url = permalink(get_absolute_url)
+
+
+Similraly, if you had a URLconf entry that looked like::
+
+ (r'/archive/(?P<year>\d{4})/(?P<month>\d{1,2})/(?P<day>\d{1,2})/$',
+ archive_view)
+
+...you could reference this using ``permalink()`` as follows::
+
+ def get_absolute_url(self):
+ return ('archive_view', (), {
+ 'year': self.created.year,
+ 'month': self.created.month,
+ 'day': self.created.day})
get_absolute_url = permalink(get_absolute_url)
+Notice that we specify an empty sequence for the second argument in this case,
+since we're only wanting to pass in some keyword arguments.
+
In this way, you're tying the model's absolute URL to the view that is used
to display it, without repeating the URL information anywhere. You can still
use the ``get_absolute_url`` method in templates, as before.
Please sign in to comment.
Something went wrong with that request. Please try again.