To start, we need a working Django project with an app that contains a model (models.py) for your content.
We will assume, you have an application within your project call tutorials. Project root directory may look something like this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14 | project_name
project_name
settings.py
urls.py
pages
templates
about.html
home.html
views.py
urls.py
tutorials
models.py
urls.py
views.py
|
Our tutorials/models.py looks something like this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26 | class TutorialManager(models.Manager):
def get_queryset(self):
return TutorialQuerySet(self.model, using=self._db)
def all(self):
return self.get_queryset().order_by('-updated_at')
class Tutorial(models.Model):
title = models.CharField(max_length=150, null=False)
description = models.TextField(max_length=300, null=True, blank=True)
created_at = models.DateTimeField(auto_now=False, auto_now_add=True)
updated_at = models.DateTimeField(auto_now=True, auto_now_add=False)
objects = TutorialManager()
class Meta:
ordering = ['-updated_at']
def __str__(self):
return self.title
def get_absolute_url(self):
kwargs = {
'pk': self.pk,
'slug': slugify(self.title)
}
return reverse("tutorials:detail", kwargs=kwargs)
|
We need to add some content to our database. You could do this using the python shell
| python manage.py shell
>>> from tutorials.models import Tutorial
>>> Tutorial.objects.create(title='Some Blog', description='Some description')
</pre><h3><strong>Step 1.</strong> Edit your applications project_name/settings.py</h3><pre class="ql-syntax" spellcheck="false">#python
INSTALLED_APPS = [
'django.contrib.sites',
'django.contrib.sitemaps',
]
#Site matching query does not exist.
SITE_ID = 1
|
NOTE: Forgetting the SITE_ID will raise an error #Site matching query does not exist.
Step 2. Edit your applications project_name/urls.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 | from django.urls import path
from django.contrib.sitemaps.views import sitemap
from django.contrib.sitemaps import GenericSitemap
from tutorials.models import Tutorial
sitemaps = {
'tutorial': GenericSitemap({
'queryset': Tutorial.objects.all(),
'date_field': 'updated_at',
}, priority=0.9),
}
urlpatterns = [
path('', include('pages.urls')),
path('tutorials/', include('tutorials.urls')),
path('sitemap.xml', sitemap,
{'sitemaps': sitemaps},
name='django.contrib.sitemaps.views.sitemap'),
]
|
Now, make migrations for the django.sites app.
| python manage.py makemigrations
python manage.py migrate
|
That's it, our dynamic sitemap.xml is ready. Visit http://localhost:8000/sitemap.xml to view your sitemap.
Step 3 & Bonus. Adding static urls to our site map.
In the our pages app, we have some static urls we want added to our sitemap.
Create a sitemaps.py file in the pages directory or where ever you choose too :). Just remember the path. We will have our sitemaps.py file in pages/sitemaps.py.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15 | from django.contrib.sitemaps import Sitemap
from django.urls import reverse
class StaticSitemap(Sitemap):
def items(self):
return ['home', 'about', 'tutorials:tutorials-list']
#tutorials:tutorials-list is from your tutorials app, assuming there is such a path
def location(self, item):
return reverse(item)
</pre><p>One final thing to do is edit the <strong><em>project_name/urls.py</em></strong> to add our static sitemap</p><pre class="ql-syntax" spellcheck="false">#python
from pages.sitemaps import StaticSitemap
sitemaps = {
'static': StaticViewSitemap,
.....
}
|
view your sitemap which now includes static urls at http://localhost:8000/sitemap.xml
Have any questions, let a comment below.
https://docs.djangoproject.com/en/3.0/ref/contrib/sitemaps/