[Not quite sure where the right place is to post this]

The general problem is that I would like to change the default output of forms across my site to have a consistent presentation that isn't of one as_p(), as_table(), or as_ul(). What I wanted to do was to add a quick function as_div() as a mixin helper class that would give me standard output across all of the forms on my site.

The challenge - Forms allow for some level of customization, but only via the self._html_output(...) operation, turns out that when you dig into this you run into two problems.

  1. The only way to create forms is with formatted print specifiers,
     which don't allow for "optional" tag generation based on
     information availability (e.g. errors, help)
  2. If you wanted to create your own _html_output() method -- via
     cut-n-paste -- you quickly discover that class BoundForm() is not
     exported from django.forms.  Which means that you now need to
     cut-n-paste that out as well.

Proposal - Allow for the extension (mixin) of the forms via the following methods:

    def _gen_normal(self, normal_row, **kwargs) :
            return normal_row % kwargs
    def _gen_error(self, error_row, toperrors) :
            return error_row % force_unicode(toperrors))

.. inside _html_output(...) change the formated prints to things like ..

  output.append(self._gen_normal(errors=force_unicode(bf_errors),
                                               label= force_unicode(label),
                                               field= unicode(bf),
                                               help_text= help_text,
html_class_attr= html_class_attr))

  output.insert(0, self._gen_error(error_row,  top_errors))

That way if you wanted to have custom output for a form you could write something like:

    def _gen_normal(self, **kwargs) :
        parts = [
                    u'<div%(html_class_attr)s>' % kwargs,
                     u'%(label)s%(errors)s<div>%(field)s</div>' % kwargs,
                ]
        if kwargs['help_text'] :
parts.append('<div><small>%s</small></div>' % kwargs['help_text'])
        parts.append('</div>')
        return ''.join(parts)

--
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