Join Login Create a Request

Implement a sitemap for a Django website

|
Implement a sitemap for a Django website

Sitemaps are crucial for a website or blog. They help search engines discover, crawl and index your blog correctly. Implementing one can be hard, especially if the sitemap is long and the data has to be paged. Django is a popular python framework and can be used to build all kinds of systems including blogs. In fact, Nerdlify is using a Django backend. 

What is a sitemap

A sitemap is a list of all the pages on a website. It shows the pages that a website wants to expose to search engines such as Google, Bing, Yandex and duckduckgo. However, search engines don't discover sitemaps on their own, you have to submit the sitemap file to the search engine manually or through a robot.txt file

Paged Sitemap

Imagine you have a Django blog, and it has 1000+ blog posts. How would you implement the sitemap so that the file is not above 2MB size?. Let's look at how to correctly page your sitemap and chunk it into small pieces that search engines can read fast.

1. Models 

Now let's start by creating models for the blog. This is a blog implementation example but the method can be used on any Django website. I'm assuming you already have a project setup and a blog app. In your Article/Post or whatever model, you have to set a get_absolute_url method as below:

def get_absolute_url(self):
        return reverse("post", kwargs={"slug": self.slug})

 And make sure you have a slug in your model fields. If you want to use ID/PK then change the slug to pk.

2. Views

The above absolute URL reference needs a URL and a view for it to work. Let's create the view first.

class ArticleDetail(DetailView):
    model = Article
    template_name = "blog/pages/article.html"
    context_object_name = "post"

    def get_context_data(self, **kwargs):
        context = super().get_context_data(**kwargs)
        return context

This is the model which will get serve individual posts. The outstanding task now is to link this view to the URL.

3. URL

Let's create the URL for our ArticleDetail view above. and since we referenced "post" as the URL name in the get_abosulte_url method don't forget to name the URL "post".

urlpatterns = [
    path('<slug>/', views.ArticleDetail.as_view(), name="post"),
]

You're now done with the models, view, and URL setup. Let's now move to the sitemaps.

4. Paged Sitemap.xml

In the main app directory URL files, define your sitemaps as follows.

from django.contrib.sitemaps import GenericSitemap
from django.contrib.sitemaps import views as sitemaps_views
from blog.models import Article

blog_info_dict = {
    'queryset': Article.objects.all().order_by('-updated_on'),
    'date_field': 'created_on',
}

class PaginatedSitemap(GenericSitemap):
    limit = 100

sitemaps = {
        'posts': PaginatedSitemap( news_info_dict, priority=0.8)
}

urlpatterns = [
#.........Other Stufff.............
  url(r'^sitemap\.xml$',
        sitemaps_views.index,
        {'sitemaps': sitemaps},
        ),
    url(r'^sitemap-(?P<section>.+)\.xml$',
        sitemaps_views.sitemap,
        {'sitemaps': sitemaps},
        name='django.contrib.sitemaps.views.sitemap'
        ),
]

Now go to site.com/sitemap.xml and you'll see the sitemap index file with all the links to sub files. Remember to set your domain name under sites in your Django dashboard. otherwise, your sitemaps will be generated with an example.com domain instead of your actual domain.


Tags.




Recent Posts

Ways to differentiate your services from others as a freelancer
Ways to differentiate your services from others as a freelancer
Ways to differentiate your services from others as a freelan..
Read More
Starting a referral or affiliate program as a freelancer.
Starting a referral or affiliate program as a freelancer.
How to start a referral program as a freelancer. These are s..
Read More
How to build referrals as a freelancer
How to build referrals as a freelancer
Ways to build your referrals as a freelancer. Expand your lo..
Read More
Handling customer Service as a Freelancer
Handling customer Service as a Freelancer
How to handle customer services as a freelancer. Learn the b..
Read More