1

I have a blog site and i want to generate 2 sitemaps, one for posts and one for categories the code that i have :

sitemaps.py

from django.contrib.sitemaps import Sitemap
from .models import Post, Category

class PostSitemap(Sitemap):
  priority = 0.5

  def items(self):
    posts = Post.objects.filter(is_published=True).order_by('-updated_at')
    return posts

  def lastmod(self, obj):
    return obj.updated_at

class CategorySitemap(Sitemap):
  priority = 0.5

  def items(self):
    categories = Category.objects.filter(
        is_published=True).order_by('-updated_at')
    return categories

  def lastmod(self, obj):
     return obj.updated_at

urls.py

from .sitemap import PostSitemap, CategorySitemap
from django.contrib.sitemaps.views import sitemap

sitemaps = {
   'posts': PostSitemap,
    'categories': CategorySitemap,
}

urlpatterns = [
    url(r'^sitemap\.xml$', sitemap, {'sitemaps': sitemaps}),
]

Is the above code a proper way to create dynamic sitemaps with django ;

Dimitris Kougioumtzis
  • 2,339
  • 1
  • 25
  • 36

1 Answers1

1

Did you figure it out? here is a simple way to generate multiple sitemap:

from django.contrib.sitemaps import GenericSitemap

from .models import Post, Category


posts_sitemap = {
    'queryset': Post.objects.filter(is_published=True).order_by('-updated_at'),
    'date_field': 'updated_at',
}

categories_sitemap = {
    'queryset': Category.objects.filter(is_published=True).order_by('-updated_at'),
    'date_field': 'updated_at',
}

sitemaps = {
    'posts': GenericSitemap(posts_sitemap, priority=0.5),
    'categories': GenericSitemap(categories_sitemap, priority=0.5),
}
longniao
  • 11
  • 1