Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Example gravatar implementation for Python 2 *and* 3, using libgravatar
# For just python:
pip install libgravatar
# libravatar documentation/options at:
# http://libgravatar.readthedocs.io/
# Then:
from libgravatar import Gravatar
g = Gravatar('myemailaddress@example.com')
g.get_image()
# (returns working URL to gravatar image)
# ----------
# For Django (as a template tag/filter), in file templatetags/mytags.py
@register.filter
def gravatar(email, size=40):
'''Get commenter's avatar from Gravatar service via API'''
g = Gravatar(email)
return g.get_image(size, "mm")
# Then in your templates:
{% load mytags %}
{# Replace number with preferred dimensions. #}
<img src="{{ comment.email|gravatar:60 }}" alt="" />
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment