Sono disposto ad usare gravatars nella mia app django. Nell'applicazione cw
ho creato una directory templatetags
con la seguente architettura:Utilizzo gravatar in django
cw/
templatetags/
__init.py__
gravatar.py
views.py
...
il gravatar.py
contiene
from django import template
import urllib, hashlib
register = template.Library()
class GravatarUrlNode(template.Node):
def __init__(self, email):
self.email = template.Variable(email)
def render(self, context):
try:
email = self.email.resolve(context)
except template.VariableDoesNotExist:
return ''
default = "/site_media/img/defaultavatar.jpg"
size = 40
gravatar_url = "http://www.gravatar.com/avatar/" + hashlib.md5(email.lower()).hexdigest() + "?"
gravatar_url += urllib.urlencode({'d':default, 's':str(size)})
return gravatar_url
@register.tag
def gravatar_url(parser, token):
try:
tag_name, email = token.split_contents()
except ValueError:
raise template.TemplateSyntaxError, "%r tag requires a single argument" % token.contents.split()[0]
return GravatarUrlNode(email)
e in uno dei modelli di cw
Ho provato:
{% load gravatar %}
ma ottengo:
'gravatar' is not a valid tag library: Template library gravatar not found, tried django.templatetags.gravatar,django.contrib.admin.templatetags.gravatar`
Corro Django 1.2.1 Python 2.6 e nel mio settings.py
:
TEMPLATE_LOADERS = (
'django.template.loaders.filesystem.load_template_source',
'django.template.loaders.app_directories.load_template_source',
'django.template.loaders.eggs.load_template_source',
)
TEMPLATE_CONTEXT_PROCESSORS = (
"django.contrib.auth.context_processors.auth",
"django.core.context_processors.debug",
"django.core.context_processors.auth",
"django.core.context_processors.request",
"django.core.context_processors.i18n",
"django.core.context_processors.media",
"django.contrib.messages.context_processors.messages",
)
EDIT: ho trovato questa altra implementazione che è più ordinato: http://tomatohater.com/2008/08/16/implementing-gravatar-django/
Hai aggiunto CW per le applicazioni installate? – zsquare
@zsquare: sì ho fatto – Mermoz
+ per il collegamento! Grazie! – bharal