Login

Gravatar support for Django comments

Author:
jonathan
Posted:
October 9, 2008
Language:
Python
Version:
1.0
Score:
4 (after 4 ratings)

A templatetag to add Gravatar support for Django comments. Based on this snippet but works for everyone who comments even if they are not a registered user.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
import urllib, hashlib
from django import template

register = template.Library()

@register.simple_tag
def gravatar(email, size=48):
    """
    Simply gets the Gravatar for the commenter. There is no rating or
    custom "not found" icon yet. Used with the Django comments.
    
    If no size is given, the default is 48 pixels by 48 pixels.
    
    Template Syntax::
    
        {% gravatar comment.user_email [size] %}
        
    Example usage::
        
        {% gravatar comment.user_email 48 %}
    
    """
    
    url = "http://www.gravatar.com/avatar.php?"
    url += urllib.urlencode({
        'gravatar_id': hashlib.md5(email).hexdigest(), 
        'size': str(size)
    })
    
    return """<img src="%s" width="%s" height="%s" alt="gravatar" class="gravatar" border="0" />""" % (url, size, size)

More like this

  1. Template tag - list punctuation for a list of items by shapiromatron 6 months, 1 week ago
  2. JSONRequestMiddleware adds a .json() method to your HttpRequests by cdcarter 6 months, 2 weeks ago
  3. Serializer factory with Django Rest Framework by julio 1 year, 1 month ago
  4. Image compression before saving the new model / work with JPG, PNG by Schleidens 1 year, 1 month ago
  5. Help text hyperlinks by sa2812 1 year, 2 months ago

Comments

Please login first before commenting.