How a sitemap looks like click here.
A sitemap can improve the crawling of your website. Also, Sitemaps will communicate your website contents to search engines, So that the search engines have an easier time indexing the contents of your website. However, using a sitemap doesn't guarantee that your web pages will be crawled and indexed, as this process is subjected to complex algorithms. But, in most cases, your website will benefit from a sitemap, and you'll never be penalized for having one.
Django comes with a high-level sitemap framework that makes creating sitemap XML files easy. In this tutorial, we will create sitemaps to include all the links to individual blog entries.
django.contrib.sites'to your INSTALLED_APPS setting.
SITE_IDas it is necessary to identify a resource in a database. Don't worry too much about it right now, just set it to 1.
Now, let's start creating our sitemap. Create a file called
sitemaps.py in any of your app. In my case, I put it in a app called
PostSitemapwhich will be a sitemap for all of our
items, which returns all the objects from a class(Post class in this case), and those objects will be included in the
Now, we have to import the
PostSitemap into our main
line 3-4, we have imported the
PostSitemap class and the
Then we have created a path, which takes in a file
sitemap view and the dictionary that we created above.
Run the server, and go to localhost:8000/sitemaps.xml, you will see the xml file. In case you get an error, Try running
python manage.py migrate command in the command prompt.
Add static pages to sitemaps.xml file. Let's start by creating a view.
A simple view function that renders a
Now, add this view to the app's
blog app in this case).
We are done with the view, It's the time to add this view to the sitemap. Open up the
And add the following:
It's the same as the previous except this time we have a function called location that returns the path of the item. We use the reverse method to get the correct path of the object.
Finally, we'll import the
StaticViewSitemap into our main
Sitemap.xml file contains the list of the web pages of your site, It helps the web crawler to more intelligently crawl your site. We use Django's sitemap framework to create a Sitemap file for a blogging system.