[Django]-Custom widget not validating only the first time

1๐Ÿ‘

I copied and pasted your code in a quick django app. Worked just fine for me. Are there other places the error could be? Django 1.4 on OS x.

views.py

class OrderedCheckboxSelectMultiple(CheckboxSelectMultiple):
    def render(self, name, value, attrs=None, choices=()):
        if value is None: value = []
        has_id = attrs and 'id' in attrs
        final_attrs = self.build_attrs(attrs, name=name)
        output = [u'<ol class="numeric">']
        # 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 = ''

            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'<li class="liAll"><label%s class="checkbox inline">%s <span class="spanLabel">%s</span></label></li>' % (
            label_for, rendered_cb, option_label))
            output.append(u'</ol>')
        return mark_safe(u'\n'.join(output))

class SomeForm(forms.Form):
    alert1 = MultipleChoiceField(choices=[(a.id, a.name) for a in Widget.objects.filter(a=False)],
                                         widget=OrderedCheckboxSelectMultiple())
    alert2 = MultipleChoiceField(choices=[(a.id, a.name) for a in Widget.objects.filter(a=False)],
                                         widget=OrderedCheckboxSelectMultiple())

def index(request):
    if request.method =="POST":
        form = SomeForm(request.POST)
        print(request.POST.keys())
        if form.is_valid():
            print("trying to save")
    else:
        form = SomeForm()
    return render_to_response('publichome.html', locals(), context_instance=RequestContext(request))

Leave a comment