Reputation: 2523
I would like to understand what is the benefit to use the get_absolute_url call instead of the url template tag.
get_absolute_url:
class Project(models.Model):
@permalink
def get_absolute_url(self):
return ('view_project', (), {'project_id': self.pk})
<a href="{{ project.get_absolute_url }}"> {{ project.name }}</a>
url template tag:
<a href="{% url 'view_project' project.pk %}"> {{ project.name }}</a>
Thank you for your help,
Julio
Upvotes: 3
Views: 2680
Reputation: 3301
I find it easier to manage all my url in template if there are "located" only into one place, ie in the model. So everytime I need an url related to an object I use get_absolute_url.
But since Django 1.5 @permalink is deprecated, you must use reverse() instead. Please check documentation
Upvotes: 0
Reputation: 53699
The only clear advantage is that you can change the name of the url for that model without having to rewrite all your templates. Also, if you define a get_absolute_url
function (you don't have to use it in your templates, though), that provides some additional benefits like adding a View on site
button in Django's admin or providing a fallback success url for class-based modelform views.
However, get_absolute_url
and in general urls for models is an ongoing point of discussion.
Upvotes: 2