DEV Community

Cover image for Using Canonical URLs for your next Django project
Osahenru
Osahenru

Posted on

Using Canonical URLs for your next Django project

What is a canonical URL?

A canonical URL is the preferred version of a web page when there are multiple URLs that could lead to the same content. A canonical URL helps search engines understand which version of a web page should be considered the primary one for indexing.

The concept of canonical URLs is important for search engine optimization (SEO) which helps prevent issues with duplicate content.

When multiple URLs point to the same content, search engines might get confused and may not know which URL to prioritize in search results.

Ensuring that each page has a canonical URL improves the clarity of your website structure to search engines. It consolidates link signals for duplicate or similar content, thus enhancing the overall ranking of your website.

Example, we could have two URLs like this http://127.0.0.1:8000/note/2024/7/18/how-to-become-more-self-disciplined/ and this http://127.0.0.1:8000/note/life-style/2024/7/18/how-to-become-more-self-disciplined/ both pointing to the same content, to avoid duplicate content issues we can choose to canonicalize the former without a life-style filter tags.

Now, you might be wondering why a page will be rendering similar or nearly similar contents. Below are some highlighted reasons why a website might have duplicate contents

  1. Language: same content(duplicate), but different languages.
  2. URL Parameters: different URL parameters might lead to the same or similar content e.g., /products/?category=shoes&sort=price vs. /products/?sort=price&category=shoes.
  3. Paginated Content: Content split across multiple pages e.g., /articles/page/2, which might appear as duplicate content.

Canonical tag

Canonical tag guides search engines in managing and prioritizing content this helps you define your primary page in search engines which in turn affects how performance metrics are tracked and analyzed.

A canonical tag is found in the head section of an HTML page

<head>
<link rel="canonical" href="..." />
</head>
Enter fullscreen mode Exit fullscreen mode

Now let’s see how we can use a canonical URL in a Django project

Writing canonical URLs in Django

Django allows you to implement the get_absolute_url() on a model class to return a canonical URL for that object

You can use the get_absolute_url() method on Django models to generate a canonical URL for objects. This method typically returns the absolute URL for a model instance, which can be used as the canonical URL. In the models.py file we can implement the get_absolute_url() method there

class Article(models.Model):
    title = models.CharField(max_length=100)
    slug = models.SlugField()
    
    publish = models.DateTimeField(default=datetime.now)

    def get_absolute_url(self):
        return reverse('note:detailed_note',
                       args=[self.publish.year,
                             self.publish.month,
                             self.publish.day,
                             self.slug ])
Enter fullscreen mode Exit fullscreen mode

and then implement this in the views by passing in the necessary arguments.

def single_note(request, year, month, day, slug):
    note = get_object_or_404(Note,
                             publish__year=year,
                             publish__month=month,
                             publish__day=day,
                             slug=slug)
Enter fullscreen mode Exit fullscreen mode

and lastly represent a canonical url in the template as shown

{% block head %} <link rel="canonical" href="{{ note.get_absolute_url }}"> {% endblock %}
Enter fullscreen mode Exit fullscreen mode

Summary

In summary, canonical URLs in Django are a best practice for managing how your content is indexed by search engines, preventing duplicate content issues, and ensuring that your website is optimized by search engines.

Top comments (0)