Re: Altering HTML output

146 views
Skip to first unread message

Steve

unread,
Jun 20, 2012, 9:03:32 PM6/20/12
to django...@googlegroups.com

widget attrs dictionary can be updated. have something else in mind?

On Jun 20, 2012 8:59 PM, "Aub" <monoli...@googlemail.com> wrote:
Hi all,

How can the html form output be altered? In my case, I just want to make the output compliant with Bootstrap. It'd be really great if someone could share their experience in trying this.

Aub

unread,
Jun 21, 2012, 8:16:19 PM6/21/12
to django...@googlegroups.com
Cheers for your reply.

We managed to do this in filter.py (see below), just not sure it's the most elegant way of doing it

class NoULCheckboxSelectMultiple(SelectMultiple):
    def render(self, name, value, attrs=None, choices=()):
        if value is None: value = []
        has_id = attrs and 'id' in attrs
        #attrs['style'] = 'display: none;'
        final_attrs = self.build_attrs(attrs, name=name)
        output = []
        # Normalize to strings
        str_values = set([force_unicode(v) for v in value])
        for i, (option_value, option_label) in enumerate(chain(self.choices, choices)):
            # If an ID attribute was given, add a numeric index as a suffix,
            # so that the checkboxes don't all have the same ID attribute.
            if has_id:
                final_attrs = dict(final_attrs, id='%s_%s' % (attrs['id'], i))
                label_for = u' for="%s"' % final_attrs['id']
            else:
                label_for = ''
            label_for = '%s  class="btn"' % label_for

            cb = CheckboxInput(final_attrs, check_test=lambda value: value in str_values)
            option_value = force_unicode(option_value)
            rendered_cb = cb.render(name, option_value)
            option_label = conditional_escape(force_unicode(option_label))
            output.append(u'<label%s>%s %s</label>' % (label_for, rendered_cb, option_label))
        return mark_safe(u'\n'.join(output))


On Thursday, June 21, 2012 3:03:32 AM UTC+2, Subsume wrote:

widget attrs dictionary can be updated. have something else in mind?

Hi all,

Robert M

unread,
Jun 21, 2012, 2:49:04 AM6/21/12
to django...@googlegroups.com
Use django-floppyforms

RM.

2012/6/21 Steve <sub...@gmail.com>

Robert M

unread,
Jun 22, 2012, 3:31:19 AM6/22/12
to django...@googlegroups.com
I'm affraid you are just making this more complicated.

Using django-floppyforms you will achieve all this by just
overriding one of these files: (creating your own in TEMPLATE_PATH/floppyforms/)


RM.

2012/6/22 Aub <monoli...@googlemail.com>
Reply all
Reply to author
Forward
0 new messages