谁能告诉我如何防止在Django模型中存储多个输入的空白​​输入字段

时间:2018-11-22 07:57:00

标签: django

在我的表单中,有多个输入字段,意味着有两个字段集,其中相同的输入字段可用,我想将这两个输入都存储在具有不同ID的模型中。它正在工作,但是当我仅填写一个输入字段并单击提交按钮时,第二个字段也存储为空白,但我想阻止它。这意味着我只想将填充的输入字段存储到模型中,而不会存储空白的输入字段。

我的Form.html

 <form class="well form-horizontal" method="post" action="{% url 'fixed_doclist' %}">
                    {% csrf_token %}
                      <fieldset>
                         <div class="form-group">
                            <label class="col-md-4 control-label">Document Name</label>
                            <div class="col-md-6 inputGroupContainer">
                               <div class="input-group"><span class="input-group-addon"><i class="glyphicon glyphicon-user"></i></span><input id="fullName" name="dname" placeholder="Full Name" class="form-control" value="" type="text"></div>
                            </div>
                         </div>

                         <div class="form-group">
                            <label class="col-md-4 control-label">Exp Date</label>
                            <div class="col-md-6 inputGroupContainer">
                               <div class="input-group"><span class="input-group-addon"><i class="glyphicon glyphicon-home"></i></span><input id="postcode" name="exp" placeholder="Postal Code/ZIP" class="form-control" value="" type="text"></div>
                            </div>
                         </div>
                         <div class="form-group">
                            <label class="col-md-4 control-label">Renewal Date</label>
                            <div class="col-md-6 inputGroupContainer">
                               <div class="input-group"><span class="input-group-addon"><i class="glyphicon glyphicon-home"></i></span><input id="postcode" name="renewdt" placeholder="Postal Code/ZIP" class="form-control" value="" type="text"></div>
                            </div>
                         </div>
                          <div class="form-group">
                            <label class="col-md-4 control-label">Purpose</label>
                            <div class="col-md-6 inputGroupContainer">
                               <div class="input-group"><span class="input-group-addon"><i class="glyphicon glyphicon-home"></i></span><input id="state" name="purpose" placeholder="State/Province/Region" class="form-control" value="" type="text"></div>
                            </div>
                         </div>

                         <div class="form-group">
                            <label class="col-md-4 control-label">Remarks</label>
                            <div class="col-md-6 inputGroupContainer">
                               <div class="input-group"><span class="input-group-addon"><i class="glyphicon glyphicon-envelope"></i></span><input id="email" name="remark" placeholder="Email" class="form-control" value="" type="text"></div>
                            </div>
                         </div>
                       </fieldset><br/><br/>



                      <fieldset>
                         <div class="form-group">
                            <label class="col-md-4 control-label">Document Name</label>
                            <div class="col-md-6 inputGroupContainer">
                               <div class="input-group"><span class="input-group-addon"><i class="glyphicon glyphicon-user"></i></span><input id="fullName" name="dname1" placeholder="Full Name" class="form-control" value="" type="text"></div>
                            </div>
                         </div>

                         <div class="form-group">
                            <label class="col-md-4 control-label">Exp Date</label>
                            <div class="col-md-6 inputGroupContainer">
                               <div class="input-group"><span class="input-group-addon"><i class="glyphicon glyphicon-home"></i></span><input id="postcode" name="exp1" placeholder="Postal Code/ZIP" class="form-control" value="" type="text"></div>
                            </div>
                         </div>
                         <div class="form-group">
                            <label class="col-md-4 control-label">Renewal Date</label>
                            <div class="col-md-6 inputGroupContainer">
                               <div class="input-group"><span class="input-group-addon"><i class="glyphicon glyphicon-home"></i></span><input id="postcode" name="renewdt1" placeholder="Postal Code/ZIP" class="form-control" value="" type="text"></div>
                            </div>
                         </div>
                          <div class="form-group">
                            <label class="col-md-4 control-label">Purpose</label>
                            <div class="col-md-6 inputGroupContainer">
                               <div class="input-group"><span class="input-group-addon"><i class="glyphicon glyphicon-home"></i></span><input id="state" name="purpose1" placeholder="State/Province/Region" class="form-control" value="" type="text"></div>
                            </div>
                         </div>

                         <div class="form-group">
                            <label class="col-md-4 control-label">Remarks</label>
                            <div class="col-md-6 inputGroupContainer">
                               <div class="input-group"><span class="input-group-addon"><i class="glyphicon glyphicon-envelope"></i></span><input id="email" name="remark1" placeholder="Email" class="form-control" value="" type="text"></div>
                            </div>
                         </div>
                       </fieldset>

                         <button>Submit</button>
               </form>

View.py文件

def fixed_doclist(request):
    print("Form is submitted successfully!")
    dname = request.POST.get("dname", False)
    exp = request.POST.get("exp", False)
    renewdt = request.POST.get("renewdt", False)
    purpose = request.POST.get("purpose", False)
    remark = request.POST.get("remark", False)

    dname1 = request.POST.get("dname1", False)
    exp1 = request.POST.get("exp1", False)
    renewdt1 = request.POST.get("renewdt1", False)
    purpose1 = request.POST.get("purpose1", False)
    remark1 = request.POST.get("remark1", False)

    DocFixed = Doc.objects.bulk_create([Doc(dname = dname, exp = exp, renewdt = renewdt, purpose = purpose, remark = remark),Doc(dname = dname1, exp = exp1, renewdt = renewdt1, purpose = purpose1, remark = remark1)])

    return render(request,'fixeddoclist.html')

Model.Py文件

class Doc(models.Model):
    dname = models.CharField(max_length=20)
    exp = models.CharField(max_length=10)
    renewdt = models.CharField(max_length=50)
    purpose = models.CharField(max_length=20)
    remark = models.CharField(max_length=10)

    def __str__(self):
        return self.dname

1 个答案:

答案 0 :(得分:0)

由于您只想创建一个Doc实例(如果提供了所有字段),因此应该检查所有字段是否都已设置。由于您可能没有两个Doc实例的数据,因此不需要批量创建。

例如:

def fixed_doclist(request):
    print("Form is submitted successfully!")

    doc_args = {
        dname: request.POST.get("dname", False), 
        exp: request.POST.get("exp", False), 
        renewdt: request.POST.get("renewdt", False), 
        purpose: request.POST.get("purpose", False), 
        remark: request.POST.get("remark", False)
    }

    doc1_args = {
        dname: request.POST.get("dname1", False), 
        exp: request.POST.get("exp1", False), 
        renewdt: request.POST.get("renewdt1", False), 
        purpose: request.POST.get("purpose1", False), 
        remark: request.POST.get("remark1", False)
    }

    if all(doc_args.values()):
        Doc.objects.create(**doc_args)

    if all(doc1_args.values()):
        Doc.objects.create(**doc1_args)

    return render(request,'fixeddoclist.html')