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

Get Django Unleashed now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.