this totally looks like a bug in crispy-forms wrap_together, I will be reviewing it this weekend or sooner if possible.
Cannot Wrap Slices From Helper with Crispy Forms in Django
-
30-05-2022 - |
Question
I'm seeking to create something as described in Displaying multiple Rows and Columns in django-crispy-forms
I have dynamic number of fields that are added to the form at run time through a parameter to the constructor. Like so :
class AddRecordForm(forms.Form):
def __init__(self, *args, **kwargs):
extra = kwargs.pop('extra')
super(AddRecordForm, self).__init__(*args, **kwargs)
self.helper = FormHelper()
self.helper.layout = Layout(extra)
self.helper.add_input(Submit('submit','Submit'))
for i, field in enumerate(extra):
self.fields[field] = forms.CharField()
In conjunction with my view, which uses ajax to return the rendered response, things seem to be nice :
form = AddRecordForm(extra=columns) #columns is a list of field names I want included in the form
context = Context( {'form' : form, 'template' : template } )
form.helper[:len(columns)].wrap(Field, css_class="span6")
#the above call works fine and wraps each field as expected
#form.helper[:len(columns)/2].wrap_together(Div, css_class="row-fluid")
#form.helper[len(columns)/2:].wrap_together(Div, css_class="row-fluid")
#print len(form.helper) => 1?
#form.helper[0][:len(columns)/2].wrap_together(Div, css_class="row-fluid")
template_string = """{% load crispy_forms_tags %} {% crispy form form.helper %}"""
t = Template(template_string)
return HttpResponse(t.render(context))
The problem occurs when I try to wrap_together the first half of the fields, and the second half of the fields. The compiler says :
list indices must be integers, not NoneType
Okay? So then I try to poke around, by then printing len(form.helper), which then outputs 1. Huh? Regardless to say, the call first addressing index 0 doesn't work either, which the compiler complaining that attribute getitem doesn't exist.
What gives? I thought I was following the [documentation][1] pretty much exactly on this use case.
EDIT #1 : Okay, there is some funkiness with splicing going on here. If I adjust the indices on the call to wrap the first half to read as such
half = len(columns)/2
form.helper[0:half].wrap_together(Div, css_class="row-fluid")
This results in ALL fields being wrapped. Not sure why it's not respecting the index end splice.
Solution