In order to use get_absolute_url, first of all you need to have named urls. 
Lets imagine we have the following named url.
....
  url(r'^accounts/(P?<pk>d+)/$',
    DetailView.as_view(
      template_name='accounts/details.html',
    ), name='account_details')
....
In order to referece this url in a template you have to alternatives, 
actually 3:
  1) You can hardcode the url, eg. /accounts/1
  2) Do a reverse url lookup, eg. {% url account_details pk=1%}
  3) Use the get_absolute_url method on your instance, eg. 
account.get_absolute_url

As you can see, using the get_absolute_url makes for a less hardcoded url; 
changin parameters in your view wouldn't mean having to go back in your 
templates and updating each one of the references to the url.

In order to define a get_absolute_url in your model you need to use the 
@permalink decorator.

@models.permalink
def get_absolute_url(self):
  return ('account_details', #name of the view you are reversing
      (), # a tuple of all the positional parameter your view takes
      {'pk':self.id}, # a dict of all the named arguments your view takes
    )



On Friday, November 16, 2012 10:03:08 AM UTC-5, enemytet wrote:
>
> How (and why) to use @models.permalink and get_absolute_url? 
>
> Please an example to better understand this. An example of the 
> documentation is not clear to me
>

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To view this discussion on the web visit 
https://groups.google.com/d/msg/django-users/-/vxjgwK838d4J.
To post to this group, send email to django-users@googlegroups.com.
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en.

Reply via email to