How To Create Canonical Url In Django

Canonical url is a url that tell search engine ( google, bing etc ) which url is the primary url for same content web pages. It is used to fix the duplicate content issue with different webpage url formats. So that search engine will know all the url that generate same web content is belongs to one canonical url. For example https://www.google.com and https://www.google.com/?abc point to the same web page content, so the canonical url for those two pages is https://www.google.com.

1. Where Can You Find The Canonical Url.

You can find one web page’s canonical url in the web page html source code head section. You can find the canonical url value in the html link tag which rel attribute’s value is ‘canonical’, and the href attribute’s value is just the url.

<link rel="canonical" href="https://www.dev2qa.com/how-to-pass-parameters-to-view-via-url-in-django/">

2. How To Generate Canonical Url In Django.

If you want to generate canonical url in Django, you can use django.urls.reverse function. Let us do some improvement on example How To Pass Parameters To View Via Url In Django. In the original example’s emp_list.html template page, we add a employee detail page url link to employee user name use django url tag. But this is not good because you should write so many code use url tag, especially when pass parameters. It is easy to make mistake.

department employee manager emp_detail html page

{% for emp in emp_list %}
    <tr>
       ......
       <td><a href="{% url 'dept_emp:emp_detail_name' user_name=emp.user.username mobile_number=emp.emp_mobile %}">{{ emp.user.username }}</a></td>
       ......
    </tr>
{% endfor %}

So to resolve above issue, we can use django.urls.reverse function in Employee model class. We define a method get_canonical_url in the DjangoHelloWorld / dept_emp / models.py file Employee model class definition section like below.

django project app dept_emp source files structure

# create Employee model, this model will be mapped to table user_register_login_employee in sqlite db.
class Employee(models.Model):
 
    ......
 
    # Return canonical url by this model object to avoid url spell error.
    def get_canonical_url(self):
        #return reverse('dept_emp:emp_detail_name', args=[self.user.username, self.emp_mobile])
        return reverse('dept_emp:emp_detail_name', kwargs={'user_name':self.user.username, 'mobile_number':self.emp_mobile})

    ......

Now we can use Employee model object’s get_canonical_url method in emp_list.html template file to generate employee detail page url link. You can use the model method to get employee user detail page link’s canonical url at any where. When the url link changes, you just need to change the get_canonical_url method, do not need to change template html source code.

{% for emp in emp_list %}
   <tr>
      ......
      <td><a href="{{ emp.get_canonical_url }}">{{ emp.user.username }}</a></td>
      ......
   </tr>
{% endfor %}

References

  1. How To Use Django url Tag And reverse() Function To Avoid Url Hard Coded

Leave a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.