Sitemaps

Django provides a sitemap framwork. To enable it:

  1. add django.contrib.sitemaps to INSTALLED_APPS and the syncdb

  2. add (r'^sitemap.xml$', 'django.contrib.sitemaps.views.sitemap', {'sitemaps': sitemaps}) to urls.py

    from project.sitemap import sitemaps
    ...
    url('^$', home, name='home'),
    url(r'^sitemap\.xml$', 'django.contrib.sitemaps.views.sitemap', {'sitemaps': sitemaps}) 
     ...
    
  3. Create a sitemap class according to the app in a file called sitemap.py:

    from django.core.urlrsolvers import reverse
    from django.contrib.sitemaps import Sitemap
    from blog.models import Entry
    
    class ViewSitemap(Siremap):
    """Reverse static views for XML sitemap."""
        def items(self):
            # Return list of url names for views to include in sitemap
            return ['home']
    
        def location(self, item):
             return reverse(item)
    
    class BlogSitemap(Sitemap):
        changefreq = "never"
        priority = 0.5
    
       def items(self):
          return Entry.objects.filter(status=1)
    
       def lastmod(self.obj):
          return obj.pub_date
    
    sitemaps = {'views': ViewSitemap,
        'blog_posts': BlogSitemap,
    }
    

Tags:

Edit this page
Wiki-logo