Reputation: 46228
I'm using django-gravatar in templates to display the avatars of my users.
Now I'd like to get the URI of an avatar from a view.
I tried to use the template tag functions like
from gravatar import templatetags
_get_gravatar_id('[email protected]')
# or
gravatar_for_email('[email protected]')
But the functions are not defined
How can I access them ?
Upvotes: 0
Views: 1125
Reputation: 549
I also wanted to generate visually pleasing, abstract, user-specific user profile images. With Sionade21's answer as my guide I implemented it as follows. I had to tweak Sionade21's answer because hashcompat is deprecated in 1.5.
Views.py:
from hashlib import md5
def my_view(request):
email_hash = md5(my_user.email.strip().lower()).hexdigest()
avatar_url = "http://www.gravatar.com/avatar/%s" % email_hash +
"?s=35&d=identicon&r=PG"
Template(s):
<img src={{ avatar_url; }}</img>
This worked great.
Eg: http://www.gravatar.com/avatar/7c40983cf4dd3b13108bc427025326c0?s=35&d=identicon&r=PG
I went one step further and saved/stored avatar_url in my database as a user attribute rather than generate it on the fly every time. I hope this helps the next person who stumbles across this question/answer.
Upvotes: 0
Reputation: 2211
It doesn't look like that project exposes the URL in python. It only provides those tags.
If you really want the image URL in python, it isn't hard to create yourself. The Gravatar site has instructions for how to create it, but basically:
from django.utils.hashcompat import md5_constructor
email_hash = md5_constructor(email.strip().lower()).hexdigest()
url = "http://www.gravatar.com/avatar/%s" % email_hash
Upvotes: 2
Reputation: 31991
You should import them.
from gravatar.templatetags.gravatar import _get_gravatar_id, gravatar_for_email
Upvotes: 1