O'Reilly logo

Django Unleashed by Andrew Pinkham

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

6.4.2 Replacing Detail Page Links with get_absolute_url()

The ultimate way to adhere to DRY while generating links is to provide a get_absolute_url() method to each model class, allowing for the quick retrieval of each model object’s canonical URL path.

6.4.2.1 get_absolute_url in Tag

Let’s focus on our Tag model class first. To reverse a URL in Python, we need the reverse() method, and so we start by importing it in Example 6.33.

Example 6.33: Project Code

organizer/models.py in dbcee003de

1   from django.core.urlresolvers import reverse

We can then focus on creating the method. The organizer_tag_detail URL pattern reads as in Example 6.34.

Example 6.34: Project Code

organizer/urls.py in dbcee003de

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required