首页 > 解决方案 > 未提交单个复选框表单。提交时也收到 MultiValueDictKeyError

问题描述

<body ng-app="">
{% extends "pmmvyapp/base.html" %}
{% load crispy_forms_tags %}
{% load static %}
{% block content%}
<div class="col-md-8">
  <form method="post" action="/personal_detail/">
  {% csrf_token %}
  <div class="form-group">
      <div class=" mb-4">
      <h6><u>Please Fill up the details below (Note that required field are marked by '*' )</u></h6>
    </div>
    <legend class="border-bottom mb-4" ,align="center">1.Beneficiary Details</legend>
    <label for="formGropuNameInput">Does Beneficiary have an Adhaar Card?*</label>
    <input type="radio" name="showHideExample" ng-model="showHideTest" value="true">Yes
    <input type="radio" name="showHideExample" ng-model="showHideTest" value="false">No
   <!--logic for yes-->
    <div ng-show="showHideTest=='true'">
      <div class="form-group">
        <label for="formGropuNameInput">Name of Beneficiary(as in Aadhar Card)*</label>
        <input name="bname" class="form-control" id="formGroupNameInput" placeholder="Enter name of Beneficiary as in Aadhar Card" required>
      </div>


      <div class="form-group">
        <label for="phone">Aadhaar Number(Enclose copy of Aadhaar Card)*:</label>
        <input name="aadharno" class="form-control" id="aadhar" name="aadhar"
               pattern="^\d{4}\s\d{4}\s\d{4}$"  placeholder="Enter Aadhar Card number with proper spacing"
               required>
      </div>
    </div>
    <!--logic for no-->
    <div ng-show="showHideTest=='false'">

      <div class="form-group">
        <label for="formGroupDistrict">Please provide any of the following Identity Card*:</label>
        <select name="idcard" id="formGroupDistrict" required>
          <option>Bank or Post Office photo passbook</option>
          <option>Voter ID Card</option>
          <option>Ration Card</option>
          <option>Kishan Photo Passbook</option>
          <option>Passport</option>
          <option>Driving License</option>
          <option>PAN Card</option>
          <option>MGNREGS Job Card</option>
          <option>Her husband's Employee Photo Identity Card issued by the government
           or any Public Sector Undertaking</option>
          <option>Any other Photo Identity Card issued by State Government or Union Territory Administrations</option>
          <option>Certificate of Identity with photograph issued by a Gazetted Officer on Official letterhead</option>
          <option>Health Card issued by Primary Health Centre(PHC) or Government Hospital</option>
          <option>Any other document specified by the State Government or Union Territory Adminstration</option>
        </select>
        </div>

        <div class="form-group">
          <label for="phone">Aadhaar Enrollment ID(EID):</label>

          <input name="eid" class="form-control" id="aadhar" name="aadhar"
                 placeholder="Enter Aadhar Enrollment ID">
        </div>


        <div class="form-group">        
          <label for="formGropuNameInput">Name of Beneficiary(as in Identity Card)* </label>
          <input name="bbname" class="form-control" id="formGroupNameInput" placeholder="Enter your name" required>
        </div>


        <div class="form-group">
          <label for="idno">Identity Number(Enclose Copy of Identity Card)*:</label>

          <input name="idno" id="identityno"  class="form-control"               
                 required>
        </div>
    </div>
 </div>


    <button type="submit" class="btn btn-primary" style="margin-bottom:10px ">Submit</button>
  </form>
</div>   
{% endblock %}
</body>

当我只填写是表格时,它不提交它只有在我填写两个表格时才会提交,我也得到一个

/personal_detail/'adhaarno' 处的 MultiValueDictKeyError

这是我的代码的views.py

视图.py

def ApplyOnline(request):
    return render(request,'users/applyonline.html')

def personal_detail(request):
    bname=request.POST['bname']
    adhaarno=request.POST['adhaarno']
   # adhaarcopy =request.POST['adhaarcopy']

    idcard=request.POST['idcard']
    eid=request.POST['eid']    
    bbname=request.POST['bname']
    idno=request.POST['idno']
   # idcopy=request.POST['idcopy']
    apply_online = Personal_Detail(bname=bname,adhaarno=adhaarno,
    idcard=idcard,eid=eid,bbname=bbname,idno=idno)
    apply_online.save()
    return render(request,'users/applyonline.html')

我已经创建了模型,对于 yes 表单名称,我将其命名为 bname,对于 no 表单名称,我将其命名为 bbname models.py

class Personal_Detail(models.Model):
    bname=models.CharField(max_length=30)
    adhaarno=models.IntegerField()
    adhaarcopy = models.ImageField(upload_to='adhaar/')

    idcard=models.TextField()
    eid=models.IntegerField()    
    bbname=models.CharField(max_length=30)
    idno=models.IntegerField()
    idcopy=models.ImageField(upload_to='identitycard/')

    def __str__(self):
        return self.bname

我认为问题出在复选框,这就是我得到 MultiValueDictKeyError 的原因。另外我应该怎么做才能只有一个表单部分,比如我只填写提交的 yes 表单,但现在我需要填写两个表单。

标签: javascriptpythonangularjsdjango

解决方案


使用ng-if指令:

<!--logic for yes-->
̶<̶d̶i̶v̶ ̶n̶g̶-̶s̶h̶o̶w̶=̶"̶s̶h̶o̶w̶H̶i̶d̶e̶T̶e̶s̶t̶=̶=̶'̶t̶r̶u̶e̶'̶"̶>̶
<div ng-if="showHideTest=='true'">
   <!-- ... -->
</div>
<!--logic for no-->
̶<̶d̶i̶v̶ ̶n̶g̶-̶s̶h̶o̶w̶=̶"̶s̶h̶o̶w̶H̶i̶d̶e̶T̶e̶s̶t̶=̶=̶'̶f̶a̶l̶s̶e̶'̶"̶>̶
<div ng-if="showHideTest=='false'">
   <!-- ... -->
</div>

ng-if指令添加和删除 DOM。该ng-show指令仅使用 CSS 更改可见性。


更新

提交时 MultiValueDictKeyError 呢?

错误

  <div class="form-group">
    <label for="phone">Aadhaar Number(Enclose copy of Aadhaar Card)*:</label>
    <input name="aadharno" class="form-control" id="aadhar" name="aadhar"
           pattern="^\d{4}\s\d{4}\s\d{4}$"  placeholder="Enter Aadhar Card number with proper spacing"
           required>
  </div>

<input>元素有两个name属性。

检查 HTML 是否有其他重复的属性。


推荐阅读