在Django中不能用脆皮表单包装来自Helper的切片



我正在寻求创建一些描述在django-crisp -forms显示多行和列

我在运行时通过构造函数的参数向表单添加了动态数量的字段。像这样:

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

结合我的视图,它使用ajax返回呈现的响应,事情似乎很好:

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

当我试图将字段的前半部分和后半部分包装在一起时,问题发生了。编译器说:

list indices must be integers, not NoneType

好吧?然后我试着四处看看,然后打印len(form.helper),然后输出1。嗯?不管怎么说,第一个寻址索引0的调用也不起作用,编译器抱怨属性getitem不存在。

给了什么?我认为我在这个用例上几乎完全遵循了[文档][1]。

编辑#1:好吧,这里有一些奇怪的拼接。如果我调整调用上的索引,将前半部分包装为这样的

half = len(columns)/2
form.helper[0:half].wrap_together(Div, css_class="row-fluid")

这会导致所有字段被包装。不知道为什么它不尊重索引结束拼接

这看起来完全像是crisp -forms wrap_together中的一个bug,如果可能的话,我将在本周末或更早的时候审查它。

最新更新