Skip to content

akarzim/image-sitemaps

Repository files navigation

image-sitemaps

Google image sitemaps for Django. More informations about image sitemaps on Google Webmaster Tools.

Installation

You can get image-sitemaps from pypi with:

pip install django-image-sitemaps

The development version can be installed with:

pip install -e git+git://github.com/akarzim/image-sitemaps#egg=image-sitemaps

image-sitemaps introduce a new XML template therefore you should add it to your INSTALLED_APPS in settings.py:

INSTALLED_APPS = (
    ...
    'imagesitemaps',
    ...
)

Usage

Create a new file named sitemaps.py in your app directory and declare your ImageSitemap classes :

# -*- coding: utf-8 -*-
from django.core.urlresolvers import reverse
import imagesitemaps

class ImageSitemap(imagesitemaps.ImageSitemap):
    """ generic data for all ours image sitempas (use it as you used to with Django Sitemap) """  
    changefreq = 'weekly'
    priority = 0.5

    def lastmod(self, obj):
        return obj.modified

class ProductImageSitemap(ImageSitemap):
    """ a specific image sitemap for ours Product class """
    
    def items(self):
        """ Django Sitemap's items method """
        return Product.objects.all()

    def images(self, obj):
        """ this method allows you to define multiple images for an object """
        return obj.images()

    def image_loc(self, img):
        """ this required method define the image location """
        return img.doc.url

    def image_caption(self, img):
        """ this optional method define the image caption """
        return unicode(img)


    def location(self, obj):
       """ Django Sitemap's location method """
        return reverse(
            'webstore_product',
            kwargs={
                'slug_product': obj.slug,
            }
        )

Then you have to add this data at the end of your Django root urls.py :

from myapp.sitemaps import ProductImageSitemap

imagesitemaps = {
    'products': ProductImageSitemap,
}

urlpatterns += patterns('imagesitemaps.views',
    url(r'^sitemap-image\.xml$', 'index', {'sitemaps': imagesitemaps, 'template_name': 'image_sitemap.xml'}),
    url(r'^sitemap-image-(?P<section>.+)\.xml$', 'sitemap', {'sitemaps': imagesitemaps, 'template_name': 'image_sitemap.xml'}),
)

Image tag definitions

Tag Required Description
<image:image> Yes Encloses all information about a single image. Each URL (<loc> tag) can include up to 1,000 <image:image> tags.
<image:loc> Yes The URL of the image.

In some cases, the image URL may not be on the same domain as your main site. This is fine, as long as both domains are verified in Webmaster Tools. If, for example, you use a content delivery network (CDN) to host your images, make sure that the hosting site is verified in Webmaster Tools OR that you submit your Sitemap using robots.txt. In addition, make sure that your robots.txt file doesn’t disallow the crawling of any content you want indexed.

<image:caption> Optional The caption of the image.
<image:geo_location> Optional The geographic location of the image. For example, <image:geo_location>Limerick, Ireland</image:geo_location>.
<image:title> Optional The title of the image.
<image:license> Optional A URL to the license of the image.

If we want to declare a new tag in our image sitemap (i.e. <image:license>), we just have to define a new method starting with image_ in our ImageSitemap class :

class ProductImageSitemap(ImageSitemap):
    ...
    def image_license(self, license):
        """ this optional method define the image license """
        return u"http://creativecommons.org/licenses/by-nd/3.0/legalcode"

About

Google image sitemaps for Django

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages