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 considere…


This content originally appeared on DEV Community and was authored by Osahenru

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>

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 ])

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)

and lastly represent a canonical url in the template as shown

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

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.


This content originally appeared on DEV Community and was authored by Osahenru


Print Share Comment Cite Upload Translate Updates
APA

Osahenru | Sciencx (2024-08-17T03:34:10+00:00) Using Canonical URLs for your next Django project. Retrieved from https://www.scien.cx/2024/08/17/using-canonical-urls-for-your-next-django-project/

MLA
" » Using Canonical URLs for your next Django project." Osahenru | Sciencx - Saturday August 17, 2024, https://www.scien.cx/2024/08/17/using-canonical-urls-for-your-next-django-project/
HARVARD
Osahenru | Sciencx Saturday August 17, 2024 » Using Canonical URLs for your next Django project., viewed ,<https://www.scien.cx/2024/08/17/using-canonical-urls-for-your-next-django-project/>
VANCOUVER
Osahenru | Sciencx - » Using Canonical URLs for your next Django project. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2024/08/17/using-canonical-urls-for-your-next-django-project/
CHICAGO
" » Using Canonical URLs for your next Django project." Osahenru | Sciencx - Accessed . https://www.scien.cx/2024/08/17/using-canonical-urls-for-your-next-django-project/
IEEE
" » Using Canonical URLs for your next Django project." Osahenru | Sciencx [Online]. Available: https://www.scien.cx/2024/08/17/using-canonical-urls-for-your-next-django-project/. [Accessed: ]
rf:citation
» Using Canonical URLs for your next Django project | Osahenru | Sciencx | https://www.scien.cx/2024/08/17/using-canonical-urls-for-your-next-django-project/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.