从多个具有相同字段名称的表单中获取所有值,而无需表单集

时间:2019-05-20 08:49:04

标签: python django

如何从具有相同字段名称的多个表单中获取所有值而没有表单集?

有我的view.py文件

...
test = str(request.POST.getlist('testing123[]'))
...

并且有html文件

<div class="input-group">
  <input type="text" class="form-control" name="testing123[]">
    <a href="javascript:void(0);" class="add_button_rip btn btn-warning" title="Add field">+</a>
</div>

使用js脚本克隆表格

<script type="text/javascript">
  $(document).ready(function(){
      var maxField = 100; //Input fields increment limitation
      var addButton = $('.add_button_rip'); //Add button selector
      var wrapper = $('.field_wrapper_rip'); //Input field wrapper
      var fieldHTML = '<div class="spacer-divide"></div><div class="input-group"><input type="text" class="form-control" name="test"><a href="javascript:void(0);" class="remove_button_rip btn btn-warning" title="Add field">-</a></div>'; //New input field html 
      var x = 1; //Initial field counter is 1

      //Once add button is clicked
      $(addButton).click(function(){
          //Check maximum number of input fields
          if(x < maxField){ 
              x++; //Increment field counter
              $(wrapper).append(fieldHTML); //Add field html
          }
      });

      //Once remove button is clicked
      $(wrapper).on('click', '.remove_button_rip', function(e){
          e.preventDefault();
          $(this).parent('div').remove(); //Remove field html
          x--; //Decrement field counter
      });
  });
  </script>

我试图以此打印

print (test)
for x in range(len(test)):
  print (test[x],end=" ")

结果只是第一个值

对不起,我的错误我在此行输入了错误的代码

var fieldHTML = '<div class="spacer-divide"></div><div class="input-group"><input type="text" class="form-control" name="test"><a href="javascript:void(0);" class="remove_button_rip btn btn-warning" title="Add field">-</a></div>';

必须是

var fieldHTML = '<div class="spacer-divide"></div><div class="input-group"><input type="text" class="form-control" name="testing123[]"><a href="javascript:void(0);" class="remove_button_rip btn btn-warning" title="Add field">-</a></div>';

1 个答案:

答案 0 :(得分:0)

test = request.POST.getlist('testing123[]')
print(test) # first check if test contains multiple elements and then start iteration over it
for t in  test:
    print(t) # you can cast it as a string now, print(str(t)). this return single element.