<i id='a7WoS'><tr id='a7WoS'><dt id='a7WoS'><q id='a7WoS'><span id='a7WoS'><b id='a7WoS'><form id='a7WoS'><ins id='a7WoS'></ins><ul id='a7WoS'></ul><sub id='a7WoS'></sub></form><legend id='a7WoS'></legend><bdo id='a7WoS'><pre id='a7WoS'><center id='a7WoS'></center></pre></bdo></b><th id='a7WoS'></th></span></q></dt></tr></i><div id='a7WoS'><tfoot id='a7WoS'></tfoot><dl id='a7WoS'><fieldset id='a7WoS'></fieldset></dl></div>
<tfoot id='a7WoS'></tfoot>

    • <bdo id='a7WoS'></bdo><ul id='a7WoS'></ul>

  • <legend id='a7WoS'><style id='a7WoS'><dir id='a7WoS'><q id='a7WoS'></q></dir></style></legend>

      1. <small id='a7WoS'></small><noframes id='a7WoS'>

      2. 动态添加字段到表单

        dynamically add field to a form(动态添加字段到表单)

            <tfoot id='XI2LT'></tfoot>

            <small id='XI2LT'></small><noframes id='XI2LT'>

            • <legend id='XI2LT'><style id='XI2LT'><dir id='XI2LT'><q id='XI2LT'></q></dir></style></legend>

                <bdo id='XI2LT'></bdo><ul id='XI2LT'></ul>
                <i id='XI2LT'><tr id='XI2LT'><dt id='XI2LT'><q id='XI2LT'><span id='XI2LT'><b id='XI2LT'><form id='XI2LT'><ins id='XI2LT'></ins><ul id='XI2LT'></ul><sub id='XI2LT'></sub></form><legend id='XI2LT'></legend><bdo id='XI2LT'><pre id='XI2LT'><center id='XI2LT'></center></pre></bdo></b><th id='XI2LT'></th></span></q></dt></tr></i><div id='XI2LT'><tfoot id='XI2LT'></tfoot><dl id='XI2LT'><fieldset id='XI2LT'></fieldset></dl></div>

                    <tbody id='XI2LT'></tbody>
                1. 本文介绍了动态添加字段到表单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我的表单中有 3 个字段.我有一个提交按钮和一个添加附加字段"按钮.我知道我可以在表单类中使用 __init__ 方法添加字段.

                  I have 3 fields in my form. I have a submit button and a button to "Add additional Field". I understand I can add fields using __init__ method in the form class.

                  我是 Python 和 Django 的新手,遇到一个初学者问题;我的问题是:

                  I am new to Python and Django and am stuck with a beginner question; my question is:

                  当我点击添加附加字段"按钮时,添加附加字段的过程是什么?

                  When I click the "Add additional field" button, what is the process to add the additional field?

                  表单是否必须再次呈现?

                  Does the form have to be rendered again?

                  如何以及何时调用 __init__ 或者我什至必须调用它?

                  How and when do I call __init__ or do I even have to call it?

                  如何将参数传递给 __init__?

                  推荐答案

                  您的表单必须基于从您的 POST 传递给它的一些变量来构建(或盲目检查属性).每次重新加载视图时都会构建表单本身,无论是否出错,因此 HTML 需要包含有关有多少字段的信息,以构建正确数量的字段进行验证.

                  Your form would have to be constructed based on some variables passed to it from your POST (or blindly check for attributes). The form itself is constructed every time the view is reloaded, errors or not, so the HTML needs to contain information about how many fields there are to construct the correct amount of fields for validation.

                  我会以 FormSet 的工作方式来看待这个问题:有一个隐藏字段包含活动表单的数量,并且每个表单名称前面都带有表单索引.

                  I'd look at this problem the way FormSets work: there is a hidden field that contains the number of forms active, and each form name is prepended with the form index.

                  其实你可以做一个单字段FormSet

                  In fact, you could make a one field FormSet

                  https://docs.djangoproject.com/en/dev/topics/forms/formsets/#formsets

                  如果您不想使用 FormSet,您始终可以自己创建此行为.

                  If you don't want to use a FormSet you can always create this behavior yourself.

                  这是一个从头开始制作的 - 它应该会给你一些想法.它还回答了您关于将参数传递给 __init__ 的问题 - 您只需将参数传递给对象构造函数:MyForm('arg1', 'arg2', kwarg1='keyword arg')

                  Here's one made from scratch - it should give you some ideas. It also answers your questions about passing arguments to __init__ - you just pass arguments to an objects constructor: MyForm('arg1', 'arg2', kwarg1='keyword arg')

                  class MyForm(forms.Form):
                      original_field = forms.CharField()
                      extra_field_count = forms.CharField(widget=forms.HiddenInput())
                  
                      def __init__(self, *args, **kwargs):
                          extra_fields = kwargs.pop('extra', 0)
                  
                          super(MyForm, self).__init__(*args, **kwargs)
                          self.fields['extra_field_count'].initial = extra_fields
                  
                          for index in range(int(extra_fields)):
                              # generate extra fields in the number specified via extra_fields
                              self.fields['extra_field_{index}'.format(index=index)] = 
                                  forms.CharField()
                  

                  查看

                  def myview(request):
                      if request.method == 'POST':
                          form = MyForm(request.POST, extra=request.POST.get('extra_field_count'))
                          if form.is_valid():
                              print "valid!"
                      else:
                          form = MyForm()
                      return render(request, "template", { 'form': form })
                  

                  HTML

                  <form>
                      <div id="forms">
                          {{ form.as_p }}
                      </div>
                      <button id="add-another">add another</button>
                      <input type="submit" />
                  </form>
                  

                  JS

                  <script>
                  let form_count = Number($("[name=extra_field_count]").val());
                  // get extra form count so we know what index to use for the next item.
                  
                  $("#add-another").click(function() {
                      form_count ++;
                  
                      let element = $('<input type="text"/>');
                      element.attr('name', 'extra_field_' + form_count);
                      $("#forms").append(element);
                      // build element and append it to our forms container
                  
                      $("[name=extra_field_count]").val(form_count);
                      // increment form count so our view knows to populate 
                      // that many fields for validation
                  })
                  </script>
                  

                  这篇关于动态添加字段到表单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  Split a Pandas column of lists into multiple columns(将 Pandas 的列表列拆分为多列)
                  How does the @property decorator work in Python?(@property 装饰器在 Python 中是如何工作的?)
                  What is the difference between old style and new style classes in Python?(Python中的旧样式类和新样式类有什么区别?)
                  How to break out of multiple loops?(如何打破多个循环?)
                  How to put the legend out of the plot(如何将传说从情节中剔除)
                  Why is the output of my function printing out quot;Nonequot;?(为什么我的函数输出打印出“无?)
                      • <bdo id='cvibc'></bdo><ul id='cvibc'></ul>

                            <tbody id='cvibc'></tbody>
                          <i id='cvibc'><tr id='cvibc'><dt id='cvibc'><q id='cvibc'><span id='cvibc'><b id='cvibc'><form id='cvibc'><ins id='cvibc'></ins><ul id='cvibc'></ul><sub id='cvibc'></sub></form><legend id='cvibc'></legend><bdo id='cvibc'><pre id='cvibc'><center id='cvibc'></center></pre></bdo></b><th id='cvibc'></th></span></q></dt></tr></i><div id='cvibc'><tfoot id='cvibc'></tfoot><dl id='cvibc'><fieldset id='cvibc'></fieldset></dl></div>

                          <tfoot id='cvibc'></tfoot>
                        1. <small id='cvibc'></small><noframes id='cvibc'>

                          <legend id='cvibc'><style id='cvibc'><dir id='cvibc'><q id='cvibc'></q></dir></style></legend>