Django sitemap: 'module' object does not have 'values' attributes

I follow the description at http://docs.djangoproject.com/en/1.2/ref/contrib/sitemaps/

I'm from django.contrib import sitemaps adds this line

(r'^sitemap\.xml$', 'django.contrib.sitemaps.views.sitemap', {'sitemaps': sitemaps}) 

in urlconf

make sitemap.py file with:

 from django.contrib.sitemaps import Sitemap from blog.models import Post class BlogSitemap(Sitemap): changefreq = 'monthly' priority = 0.5 def items(self): return Post.objects.all() def lastmod(self, obj): return obj.date 

at this address http://127.0.0.1:8000/sitemap.xml I get an error message:

 Traceback: File "/usr/lib/python2.7/site-packages/django/core/handlers/base.py" in get_response 100. response = callback(request, *callback_args, **callback_kwargs) File "/usr/lib/python2.7/site-packages/django/contrib/sitemaps/views.py" in sitemap 33. maps = sitemaps.values() Exception Type: AttributeError at /sitemap.xml Exception Value: 'module' object has no attribute 'values' 

Can anybody help me?

+7
source share
2 answers

You skipped a step - see the example in the documentation .

Instead of importing the sitemaps module into urls.py, import your BlogSitemap class and then create a sitemaps dictionary:

 sitemaps = {'blog': BlogSitemap} 
+7
source

I ran into this problem. Between the documentation and the code samples that I was looking at, I still could not understand why I saw this error.

The wrong documentation for me was from https://docs.djangoproject.com/en/dev/ref/contrib/sitemaps/ :

It can also display an instance of the Sitemap class (e.g. BlogSitemap (some_var)).

Then I looked at the source of Django a little closer. The view is as follows (django.contrib.sitemaps.views.sitemap):

 def sitemap(request, sitemaps, section=None, template_name='sitemap.xml'): maps, urls = [], [] if section is not None: if section not in sitemaps: raise Http404("No sitemap available for section: %r" % section) maps.append(sitemaps[section]) else: maps = sitemaps.values() # This is where I was seeing the error. page = request.GET.get("p", 1) current_site = get_current_site(request) for site in maps: try: if callable(site): urls.extend(site().get_urls(page=page, site=current_site)) else: urls.extend(site.get_urls(page=page, site=current_site)) except EmptyPage: raise Http404("Page %s empty" % page) except PageNotAnInteger: raise Http404("No page '%s'" % page) xml = smart_str(loader.render_to_string(template_name, {'urlset': urls})) return HttpResponse(xml, mimetype='application/xml') 

Then it became clear to me that the sitemaps parameter is actually a dictionary of the key to Sitemap objects, and not a sitemap object. Perhaps this was obvious to me, but it took me a bit to overcome my mental block.

The full coding example that I used is as follows:

sitemap.py 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 

Urls.py 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}), ) 
+6
source

All Articles