Yawd website

jun30

How to add sitemap functionality in websites powered by django-articles

Django-articles is an open-source blogging engine written in django. Unfortunately it does not offer a sitemap mechanism and since the yawd blog uses django-articles I had to implement one. If you happen to maintain a django-articles powered website and need a sitemap xml file here is how to do it:

Download the sitemaps.py file attached to this article and place it under your project's main application. This module uses django's built-in sitemaps framework to define a set of sitemap classes. In your settings.py file enable the django.contrib.sitemaps application:

settings.py:
INSTALLED_APPS = (
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.humanize',
    'django.contrib.markup',
    'django.contrib.messages',
    'django.contrib.sessions',
    'django.contrib.sites',
    'django.contrib.syndication',
    'django.contrib.sitemaps',
    'articles',
    .....
)

Make also sure that django.template.loaders.app_directories.Loader is set in your TEMPLATE_LOADERS:

settings.py
TEMPLATE_LOADERS = (
    'django.template.loaders.filesystem.Loader',
    'django.template.loaders.app_directories.Loader',
    ...
)

By now the django sitemap functionality must be enabled in your website. To exploit its capabilities we must define a dictionary containing the sitemap classes we will use. In the sitemaps.py module you just downloaded, the following classes are implemented:

ArticleSitemap - A sitemap class providing the links to all live articles
TagSitemap - Get links of pages that enlist all articles related to a tag
AuthorsSitemap - A sitemap class that returns the links to all authors' articles (grouped by author)
ArchiveSitemap - The link to the 'archives' view (typically the homepage)
DateSitemap - A sitemap class that provides all links of articles grouped by date

In yawd blog both the 'Tag cloud' and the 'Article archives' are enabled so I use all the above classes. In your urls.py module define the dictionary as follows, ommiting the classes you won't need:

urls.py
from yawdblog.sitemaps import *

sitemaps = { 'articles' : ArticleSitemap,
            'tag' : TagSitemap,
            'authors' : AuthorsSitemap,
            'archive' : ArchiveSitemap,
            'dates' : DateSitemap }

Now associate the sitemap dictionary with django's built-in view to get your xml file. In the urlpatterns list of your urls.py module add the following record:

urls.py:
urlpatterns = patterns('',
    ...
    (r'^sitemap\.xml$', 'django.contrib.sitemaps.views.sitemap', {'sitemaps': sitemaps}),
    ...
)

Note:In the attachments section you can will find a sample urls.py file to help you find your way.

We're almost done! Restart your apache, visit yourdomain.com/sitemap.xml and you should see your sitemap xml. Optionally you can ping google to notify them about your sitemap following these instructions.

Attachments

Comments powered by Disqus