在django中使用gravatar

在django中使用gravatar,django,django-templates,gravatar,Django,Django Templates,Gravatar,我愿意在我的django应用程序中使用。 在应用程序cw中,我创建了一个具有以下体系结构的templatetags目录: cw/ templatetags/ __init.py__ gravatar.py views.py ... gravatar.py包含 from django import template import urllib, hashlib register = template.Library() class

我愿意在我的django应用程序中使用。 在应用程序
cw
中,我创建了一个具有以下体系结构的
templatetags
目录:

cw/
    templatetags/
         __init.py__
         gravatar.py
    views.py
    ...
gravatar.py
包含

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)
cw
的一个模板中,我尝试了:

{% load gravatar %}
但我得到:

'gravatar' is not a valid tag library: Template library gravatar not found, tried django.templatetags.gravatar,django.contrib.admin.templatetags.gravatar`
我在我的
settings.py
中运行django 1.2.1 python 2.6:

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",
)

编辑:我发现了另一个更简洁的实现:

因此我找到的解决方案是通过创建一个lib目录,在我的所有应用程序之间共享gravatar模板:

proj/
    __init__.py
    lib/
        __init__.py
        templatetags/
            __init.py__
            common_tags.py
将lib添加到我安装的应用程序中

您的问题在于:

cw/
    templatetags/
        __init.py__ <<<
        gravatar.py
    views.py
    ...
cw/
模板标签/

__init.py_________________________________?