Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When to use Django get_absolute_url() method?

Tags:

python

django

Django documentation says:

get_absolute_url() method to tell Django how to calculate the canonical URL for an object.

What is canonical URL mean in this is context?

I know from an SEO perspective that canonical URL means picking the best URL from the similar looking URLs (example.com , example.com/index.html). But this meaning doesn't fit in this context.

I know this method provides some additional functionality in Django admin, redirection etc. And I am fully aware of how to use this method.

But what is the philosophy behind it? I have never actually used it in my projects. Does it serve any special purpose?

like image 548
Cody Avatar asked Apr 03 '17 08:04

Cody


People also ask

What is the use of Get_absolute_url in Django?

get_absolute_url allows to keep your object DRY. To have a url which defines that object instance. In most case a detail page for that object.

What is the purpose of __ Str__ method in Django?

str function in a django model returns a string that is exactly rendered as the display name of instances for that model. # Create your models here. This will display the objects as something always in the admin interface.

How can I get full URL in Django?

Use handy request. build_absolute_uri() method on request, pass it the relative url and it'll give you full one. By default, the absolute URL for request. get_full_path() is returned, but you can pass it a relative URL as the first argument to convert it to an absolute URL.

What is the use of slug in Django?

Slug is used in Django to dynamically generate a human-friendly/readable URL.


2 Answers

First of all, when it comes to web development you really want to avoid hard coding paths in your templates. The reason for this is that paths might change, and it will be a hassle to go through all your HTML and templates to find every single URL or path and update it manually. It makes your code much harder to maintain.

The solution to this is to define functions that return the URL instead. This is where get_absolute_url() comes into the picture.

Example:

<!-- Bad --> <a href="/language/category/product/{{product.pk}}">Link</a>  <!-- Good --> <a href="{{product.get_absolute_url}}">Link</a> 

Canonical URL

Your second question is what a canonical URL is. A canonical URL is "the official" url to a certain page. Sometimes an asset can be displayed in multiple different URL's, for example:

/en/shoes/1-nike-shoes/ /en/shoes/1-nike-shoes?sort=price&order=asc /en/shoes/1-nike-shoes?sort=price&order=desc 

Here we have the same asset displayed in 3 different URL's. The "Canonical URL" would be the one we defined as the main one. E.g. /en/shoes/1-nike-shoes/.

Its very useful to define what a official or "main" URL to a certain asset is. It will allow you to prevent duplicate content when search engines index your website.

In the context of the quote you are using from the Django Documentation. "Canonical" in this case means "the official URL where this model is displayed".

like image 132
Marcus Lind Avatar answered Sep 21 '22 06:09

Marcus Lind


See this example:

I want to generate a unique URL for each product, the pattern will be - http://...../products/abcdefg/ (after '/products/' it's my slug value).

In models.py, I created the 'get_absolute_url' that will generate url in the above pattern

models.py

class Products(models.Model):     title = models.CharField(max_length=120)     slug = models.SlugField(blank=True, unique=True)     description = models.TextField()      def get_absolute_url(self):         return f"/products/{self.slug}/" 

productlist.html - get_absolute_url referred in anchor tag

{% for obj in object_list  %}    <a href="{{obj.get_absolute_url}} ">{{obj.title}}</a> <br> {% endfor %} 

urls.py - my url pattern

 path('/products/<slug>/', ProductDetails.as_view()), 
like image 29
Amit Baderia Avatar answered Sep 20 '22 06:09

Amit Baderia