Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/clojure/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 无法使用Django中的脆表单包装来自Helper的切片_Python_Django_Django Forms_Django Crispy Forms - Fatal编程技术网

Python 无法使用Django中的脆表单包装来自Helper的切片

Python 无法使用Django中的脆表单包装来自Helper的切片,python,django,django-forms,django-crispy-forms,Python,Django,Django Forms,Django Crispy Forms,我正在寻求创建如中所述的内容 我有动态数量的字段,这些字段在运行时通过构造函数的参数添加到表单中。像这样: class AddRecordForm(forms.Form): def __init__(self, *args, **kwargs): extra = kwargs.pop('extra') super(AddRecordForm, self).__init__(*args, **kwargs) self.helper = FormHelper

我正在寻求创建如中所述的内容

我有动态数量的字段,这些字段在运行时通过构造函数的参数添加到表单中。像这样:

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返回渲染响应的my view,情况似乎很好:

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

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

这看起来完全像是一个用脆表单包装起来的bug,我将在本周末或可能的话更早的时候对它进行审查。

任何见解都将不胜感激,目前只使用香草django表单,但我真的很想使用它!)现在已在
dev
分支中修复了此问题,很抱歉延迟。它将很快在1.3.0版本下发布,感谢您的跟进,如果有任何进一步的问题,将继续跟进!