3

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

2 Answers 2

2

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.

Sign up to request clarification or add additional context in comments.

1 Comment

I think he is right, you can do a lot implementing the function.. docs.djangoproject.com/en/dev/ref/models/instances/…
0

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.