我参考了这个网站: http ://tomatohater.com/2008/08/16/implementing-gravatar-django/
我有一个配置文件应用程序,其中有一个带有 mytags.py 的 templatetags 目录。
mytags.py 的内容
import urllib, hashlib
from django import template
register = template.Library()
@register.inclusion_tag('templatetags/gravatar.html')
def show_gravatar(email, size=48):
default = "http://www.mysite.com/media/images/no-avatar.gif"
url = "http://www.gravatar.com/avatar.php?"
url += urllib.urlencode({
'gravatar_id': hashlib.md5(email).hexdigest(),
'default': default,
'size': str(size)
})
return {'gravatar': {'url': url, 'size': size}}
模板/templatetags/gravatar.html 的内容
<img src="{{ gravatar.url }}" width="{{ gravatar.size }}" height="{{ gravatar.size }}" border="0" />
该标签在 profile.html 模板文件中使用。
但是头像没有显示在profile.html中。然而消息来源显示
<div class="gravatar">
<a href="">
<img src="http://www.gravatar.com/avatar.php?default=http%3A%2F%2Fwww.mysite.com%2Fmedia%2Fimages%2Fno-avatar.gif&size=48&gravatar_id=5d1b0b64499475516bd9c051aa7f3560" width="48" height="48" border="0" />
</a></div>