5 Steps to create a sitemap in DjangoA sitemap is a file where we provide information about the web pages and other files of our site. Search engines like Google, Bing etc. read this file to more intelligently crawl our site.
It is a XML file that holds complete list of page URLs for a site along with other additional details (metadata of each URLs, when it was last updated etc.). It 's primary purpose is to inform search engines about pages on your sites that are available for crawling.
Sitemap is very helpful to increase your website' s rank on google.
Today i will show you that how can you create a xml sitemap of your django website easily. If you are using django for web development then you don't require any 3rd party tool to generate a sitemap for your website. It' s a super easy task to make a sitemap in django. I will keep the things as simple as possible and explain the whole procedure step by step.
Step 1 In settings.py file add
'django.contrib.sites' & 'django.contrib.sitemaps' in INSTALLED_APPS list and make a variable SITE_ID=1 under INSTALLED_APPS list as shown below :
Now, run the migrate command :
python manage.py migrate
Step 2 Create a sitemaps.py file in any app and write the code as shown below :
from django.contrib.sitemaps import Sitemap
from blog.models import Post
Here i take an example of a blog app. I have just imported the Post model class from blog (you can use import * if you have more than 1 model).
Always remember that you have to make 1 class in sitemaps.py (of any name ,here PostSitemap) for each table (model) and define a "items" function in this class which will return all objects from the table.
Step 3 In urls.py file make a dictionary sitemaps and put all the class objects (from sitemaps.py) in it and assign a url pattern for your website's sitemap in urlpatterns list as shown below -
from app.sitemaps import PostSitemap
#put all sitemaps in this dictionary }
For this case the sitemap will be shown when we use the url
Step 4 In each and every model class which you want to include in sitemap, you have to put an get_absolute_url function .
The above code will work for an url something like this :
If the url uses parameters/arguments then you have to pass these arguments in "args" list in "get_absolute_url" function.
Step 5 Now open the django admin app and move to Sites table to add and save a record for your website as shown below :
If you want to learn how to create robots.txt file in django, you should read this article on how to generate robots.txt file in django
Some recent posts
Complete process of changing database from SQLite to MySQL - How to migrat...
" How to download a file in django | Making a large file downloadable from ...
How to use proxy in python requests | http & https proxies - Scraping...
Top Django Querysets, How to filter records | Fetching data from database e...
How to change base url or domain in sitemap - Django ...
How to Make a Website - Everything you should know about web development...
Top 5 Interview Questions : Tips for HR round ...
How to get job in IT : Perfect resume guide for IT job ...
Programming vs coding | difference between programming and coding ...
Popular PostsHow to compress image before saving to DB
Migrating from SQLite to MySQL in Django
Choosing correct M.2 SSD
How to deploy a web app on pythonanywhere
5 Steps to create sitemap in Django
How to send mails automatically in Django
Handling arguments in django urls
Media and Static configuration in Django
Language Translation & detection using python
React Js vs Angular Js
5 Tips to improve Programming Logics