Video and image sitemaps in Django

I recently added a sitemap to a site with many images and videos. Having the media files in the sitemap helps search engines to index them. I hadn't done this with Django before, so here are some notes.

If you're not familiar with sitemaps in Django check out the official documentation and create a sitemap for a model with media files.

Adding Videos

To customize the output and to add videos to the default sitemap you'll want to create a custom template as described in the documentation. I always copy the default template from contrib/sitemaps/templates/sitemap.xml You can access your model instances through url.item. Here's an example for a video model that's connected to an item through foreign keys:

{% load thumbnail %}
{% for video in url.item.video_set.all %}
        {% thumbnail video.image "662x372" crop="center" as thumb %}
            <video:thumbnail_loc>{{ MEDIA_URL }}{{ thumb }}</video:thumbnail_loc>
        {% endthumbnail %}
        <video:title>{{ url.item.title }}</video:title>
        {% if url.item.description %}
            <video:description>{{ url.item.description|striptags }}</video:description>
        {% endif %}
        <video:content_loc>{{ MEDIA_URL }}{{ video.file }}</video:content_loc>
        <video:publication_date>{{ url.item.pub_date|date:'c' }}</video:publication_date>
{% endfor %}

Include something simliar to this inside the <url> tag. I think this site uses sorl-thumbnail, so you'll probably want to modify the thumbnail section at least.

One problem I had were invalid timestamps. The sitemap specs are very specific, either YYYY-MM-DD or YYYY-MM-DDThh:mm:ss+TZD. The first format is trivial with the date template tag.

But the second one needs Django's timezone support for the timezone info. I guess you could hardcode the time zone offset into your template though. Using the date template tag with O does not work.

Adding Images

Adding images is simpler:

{% for image in url.item.image_set.all %}
        <image:loc>{{ MEDIA_URL }}{{ image.file }}</image:loc>
{% endfor %}


Don't forget to add the relevant XML namespace declarations by using the xmlns attribute. I strongly suggest that you validate your sitemaps. Google's Webmaster Tools can do that, see under Optimzation once you've added your site. If you haven't done that yet you might be interested in my webmaster verification app.



Cancel reply
Markdown. Syntax highlighting with <code lang="php"><?php echo "Hello, world!"; ?></code> etc.
DjangoPythonBitcoinTuxDebianHTML5 badgeSaltStackUpset confused bugMoneyHackerUpset confused bugX.OrggitFirefoxWindowMakerBashIs it worth the time?i3 window managerWagtailContainerIrssiNginxSilenceUse a maskWorldInternet securityPianoFontGnuPGThunderbirdJenkinshome-assistant-logo