On Jun 3, 12:00 pm, Bill Freeman <ke1g...@gmail.com> wrote:
> If you can't use _html_output, then you have to duplicate a lot of
> it's functionality.

I'm able to get pretty far with the template tag approach. The trouble
is that BoundForms offer no way to directly add attributes, which I
think can only be included when defining the form field in question.

# from ... import format as form_format

@register.simple_tag
def format_form(form):
  html = ''

  field_maps = {
    forms.CharField: form_format.text,
    forms.ChoiceField: form_format.select
  }

  for name, field in form.fields.items():
    for cls, handler in field_maps.items():
      if isinstance(field, cls):
        html += handler(name, forms.forms.BoundField(form, field,
name))
  return html

# in format.py
def flatten(items):
  return ''.join(items)

def wrapper(name, field, type, wrapped):
  return '<div class="input %s">%s</div>' % (type, flatten(wrapped))

def label(name, field):
  # First bit of duplication
  if not field.label.endswith(':'):
    field.label += ':'
  return field.label_tag()

def text(name, field):
  return wrapper(name, field, 'text', (
    label(name, field),
    unicode(field)
  ))

def select(name, field):
  return wrapper(name, field, 'select', (
    label(name, field),
    unicode(field)
  ))

Thomas

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-us...@googlegroups.com.
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en.

Reply via email to