- Jh123x: Blog, Code, Fun and everything in between./
- My Blog Posts and Stories/
- New Sitemap (Generating a Sitemap in Django)/
New Sitemap (Generating a Sitemap in Django)
Table of Contents
New feature added #
Site map
What happened #
While I was checking up on the website, I noticed that there were a bunch of people who are filling up the contact form to offer paid services for SEO optimization.
What is SEO and Why should I care? #
SEO stands for Search Engine Optimization. It helps search engines like google display your webpage to other users who are searching for it online.
In a way you can think of it as making a website more attractive to search engines.
By having a good SEO, it makes it more likely for it to appear as a higher result on a particular search. It is a good way to generate organic views for a website (Such as this one).
To learn more about SEOs, you can visit the link here
This are the things I can do to improve my SEO
- Add a site map
- Add metadata to the website
- Keywords / Phrases
- Summary of the page
- Add alt tags to images
- Publish more relevant content
- Update content regularly
- Attract links from other websites
There are some of them which I can work on in my website and some others which depend on other people who find my blog interesting. With that in mind I decided to do each of them slowly one step at a time as there are many changes which has to be made for each of them to be implemented.
Maybe I should have thought of this earlier to avoid the hassle I have to deal with now.
What is a sitemap? #
A sitemap is a file where we provide information about the pages and files on our website and the relationship between them.
This allows search engines like google to crawl a site more effectively. It tells them which files are the main ones to focus on.
For more information check out this
Time to dive into the details (Implementing it in Django) #
Lucky for me, there is a built in middleware in Django that allows me to generate a site map. It is called the sitemaps framework
.
Step 1: Installation #
To install the application into your django app, add the following to INSTALLED_APPS
under your settings.py
file as shown below.
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'django.contrib.sitemaps', # Add this
]
SITE_ID = 1 # Add this as well
What does the SITE_ID
do? It marks the domain id for the website in case the Django application is serving multiple websites.
Step 2: Adding the path #
To start, add the following to your urls.py
file. This will make the /sitemap.xml
path point to the generate site map file
# config/urls.py
from django.contrib import admin
from django.urls import path, include
from django.contrib.sitemaps.views import sitemap
## We will modify this later to add the site maps in. We can leave this blank for now
sitemaps = {
}
urls = [
...,
path('sitemap.xml', sitemap, {'sitemaps': sitemaps}, name='django.contrib.sitemaps.views.sitemap') # Add the path to the sitemap
]
Adding the Sitemap #
To add a sitemap, we will need to create a new class which inherits from the Sitemap
superclass and put it into the
There are different types of pages to create the sitemap. Generally they are split into two categories - static and dynamic.
For each of there there is a priority and a change frequency.
- The priority tells is the site map which ones are the most important pages on the website.
- The Change frequency tells the search engine how often the page is updated.
Static Pages #
For static pages, we can define a fixed Sitemap class. In this case, I am creating a site map for my homepage.
#/homepage/sitemap.py
from django.contrib.sitemaps import Sitemap
from django.urls import reverse
class HomepageSitemap(Sitemap):
priority = 1.0
changefreq = 'yearly'
def items(self): # Tells them which function(s) is the sitemap for.
return ['index']
def location(self, item): # Looks for the url path of the function.
return reverse(item)
This will show up as
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" xmlns:xhtml="http://www.w3.org/1999/xhtml">
<url>
<loc>http://127.0.0.1:8000/</loc>
<changefreq>yearly</changefreq>
<priority>1.0</priority>
...
</url>
</urlset>
Dynamic Pages #
For dynamic pages, we can define a dynamic Sitemap class. In this case, I am creating the site map for the blog.
#/blog/sitemap.py
from django.contrib.sitemaps import Sitemap
from blog.models import Entry
class BlogSitemap(Sitemap):
changefreq = "never"
priority = 0.5
def items(self):
return Entry.objects.filter(is_draft=False) # Getting all blog posts which are not drafts
def lastmod(self, obj):
return obj.last_modified_date # This is the last modified date of the blog post
In this case, the items are the blog entries in the model and the lastmod is the last modified date for the blog.
This will eventually show up as the following assuming that I have 2 blog post with id 10
and 11
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" xmlns:xhtml="http://www.w3.org/1999/xhtml">
<url>
<loc>http://127.0.0.1:8000/blog/10</loc>
<lastmod>2022-03-07</lastmod>
<changefreq>yearly</changefreq>
<priority>0.7</priority>
...
</url>
<url>
<loc>http://127.0.0.1:8000/blog/11</loc>
<lastmod>2022-03-07</lastmod>
<changefreq>yearly</changefreq>
<priority>0.7</priority>
...
</url>
</urlset>
Adding the pages in #
Now that the sitemap is created for the homepage and the blog, we can proceed to add it in the urls.py
file from earlier.
# config/urls.py
...
from blog.sitemaps import BlogSitemap
from homepage.sitemaps import HomepageSitemap
## We will modify this later to add the site maps in. We can leave this blank for now
sitemaps = {
'blog': BlogSitemap(), # Add the blog sitemap in
'homepage': HomepageSitemap(), # Add the homepage sitemap in
}
...
After adding it in you should be able to find the sitemap in the /sitemap.xml
route. In the case for my website its here
Conclusion #
I will be doing more to improve the SEO of the website. Hopefully this will help me become more famous and well know hur hur hur. Next time I can do something more about the SEO. See you guys on the first page of google :D. Till next time :D.