from django.template import Library
from django.newforms.util import flatatt

register = Library()

@register.inclusion_tag("forms/label.html")
def label(field, contents=None, attrs=None):
    if isinstance(attrs, basestring):
        attrs = [tuple(pair.split('=')) for pair in attrs.split(',')]
    attrs = dict(attrs or {})
    widget = field.field.widget
    for_id = widget.id_for_label(widget.attrs.get('id') or field.auto_id)
    contents = contents or field.label
    return {'field': field, 'id': for_id, 'contents': contents, 'attrs': attrs}

@register.filter
def attrlist(attrs):
    attrs = dict(attrs)
    return flatatt(attrs)
attrlist.is_safe = True

# My "forms/label.html" looks like this...
"""
{% load forms %}

{% if not field.is_hidden %}
{% with field.field.required as required %}
<label for="{{ id }}"{{ attrs|attrlist }}{% if required and not attrs.class %} class="required"{% endif %}>
    {{ contents }}
</label>
{% endwith %}
{% endif %}
"""