Previous Page Next Page

Pinging Google

Django also provides a simple way to ping Google to notify it that your web content has changed and that your site should be reindexed. The django.contrib.sitemaps.ping_google() function tells Google to reindex your site's sitemap.

The default value for the sitemap is '/sitemap.xml'. However, you can specify a different value by using the sitemap_url argument. For example:

ping_google(sitemap_url='/sitemap-report.xml')

By the Way

The ping_google() function raises the django.contrib.sitemaps.SitemapNotFound exception if it cannot determine the website.


Using the ping_google() function, you can notify Google every time the data in your database changes. The following example pings Google every time the report object is saved:

from django.contrib.sitemaps import ping_google
class report(models.Model):
    . . .
    def save(self):
        super(report, self).save()
        try:
            ping_google(sitemap_url='/sitemap-report.xml')
        except Exception:
            pass

Watch Out!

If objects are being saved frequently, you may not want to add the extra cycles that the ping_google() function generates. Pick objects that do not get updated as frequently, or add a timed function that runs periodically to ping Google.


Previous Page Next Page