import hashlib
import urllib
from django import template
from django.utils.safestring import mark_safe
register = template.Library()
# return only the URL of the cravatar
# TEMPLATE USE: {{ email|cravatar_url:150 }}
@register.filter
def cravatar_url(email, size=40):
default = "https://example.com/static/images/defaultavatar.jpg"
return "https://cravatar.cn/avatar/%s?%s" % (hashlib.md5(email.lower()).hexdigest(), urllib.urlencode({'d':default, 's':str(size)}))
# return an image tag with the cravatar
# TEMPLATE USE: {{ email|cravatar:150 }}
@register.filter
def cravatar(email, size=40):
url = cravatar_url(email, size)
return mark_safe('<img src="%s" height="%d" width="%d">' % (url, size, size))
Django 图像请求
文档最后修改于
由文派文库团队编撰