by John M Costa, III


I ran into an issue trying to generate a sitemap.xml file with Django’s built-in sitemap view. After reading the documentation over a few times I still received the error:

Django sitemap: 'module' object has no attribute 'values'
For some reason, it wasn't obvious to me what the confusion was, but clearly others have encountered the issue as well. A quick google search turned up a question on StackOverflow. User KuB had asked a question which looked very similar to the one I had just experienced.

After a bit of research and experimentation I was able to resolve the issue and came up with a bit of code that resolved the issue for me:

The full coding sample that I used looks like the following: file:

from django.contrib.sitemaps import Sitemap
from articles.models import Article

class BlogSitemap(Sitemap):
    changefreq = "never"
    priority = 0.5

    def items(self):
        return Article.objects.filter(is_active=True)

    def lastmod(self, obj):
        return obj.publish_date file:

from sitemap import BlogSitemap

# a dictionary of sitemaps
sitemaps = {
    'blog': BlogSitemap,

urlpatterns += patterns (''
    #...<snip out other url patterns>...
    (r'^sitemap\.xml$', 'django.contrib.sitemaps.views.sitemap', {'sitemaps': sitemaps}),

You can find the full thread out on Stack Overflow here.

comments powered by Disqus