XP on Rails Extreme Programming Blog

26Apr/090

Django gravatar tag

This short tutorial shows how to create a gravatar tag in a Django application.

Gravatar is an useful application that lets you to associate an avatar to your email address.
So you can show the same avatar in a large number of web sites without configure it every time.

First of all, we need to write the python code an register the “gravatar” tag:

from django import template

import urllib, hashlib

register = template.Library()

def gravatar(email, size=80):
    gravatar_url = "http://www.gravatar.com/avatar.php?"
    gravatar_url += urllib.urlencode({
        'gravatar_id':hashlib.md5(email).hexdigest(),
        'size':str(size)})
    return """<img src="%s" alt="gravatar for %s" />""" % (gravatar_url, email)

register.simple_tag(gravatar)

Then we can use the new gravatar tag in all templates page, by simply typing:

{% load gravatar %}

{% gravatar user.email 70 %}

About stefano

Independent Information Technology and Services Professional
Comments (0) Trackbacks (0)

No comments yet.


Leave a comment


No trackbacks yet.