/ personal_detail /中的IntegrityError

时间:2020-02-24 16:21:11

标签: python django

我收到MultiValueKeyDictError时遇到了这个错误,我使用了POST.get但比我得到了这个错误

IntegrityError at /personal_detail/
NOT NULL constraint failed: users_personal_detail.beneficiary_adhaar_name
Request Method: POST
Request URL:    http://127.0.0.1:8000/personal_detail/
Django Version: 3.0.3
Exception Type: IntegrityError
Exception Value:    
NOT NULL constraint failed: users_personal_detail.beneficiary_adhaar_name
Exception Location: C:\Python\Python38\lib\site-packages\django\db\backends\sqlite3\base.py in execute, line 396
Python Executable:  C:\Python\Python38\python.exe
Python Version: 3.8.1
Python Path:    
['C:\\pradhan-mantri-matru-vandana-yojana\\pmmvy',
 'C:\\Python\\Python38\\python38.zip',
 'C:\\Python\\Python38\\DLLs',
 'C:\\Python\\Python38\\lib',
 'C:\\Python\\Python38',
 'C:\\Users\\diwas\\AppData\\Roaming\\Python\\Python38\\site-packages',
 'C:\\Python\\Python38\\lib\\site-packages']

我的views.py看起来像这样,我使用“ get”来避免出现MultiValueKeyDictError

def ApplyOnline(request):
    return render(request,'users/applyonline.html')
@login_required
def personal_detail(request):
   # ShowHideExample = request.POST.get('showHideExample',False)
    beneficiary_adhaar_name=request.POST.get('beneficiary_adhaar_name')
    adhaarno=request.POST.get('adhaarno')
   # adhaarcopy =request.POST['adhaarcopy']

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

models.py(我尝试使用idno = request.POST.get('idno',null = True),但它不起作用)

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


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

    def __str__(self):
        return self.beneficiary_adhaar_name + self.beneficiary_id_name

我尝试在idcard = request.POST.get('idcard',null = True)之类的视图中使用nulll = False,但是再次遇到无法与null或类似内容关联的错误。

applyonline.html

<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>(*Mandatory Fields)Please Fill up the details below </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-if="showHideTest=='true'">
      <div class="form-group">
        <label for="formGropuNameInput">Name of Beneficiary(as in Aadhar Card)*</label>
        <input name="beneficiary_adhaar_name" class="form-control" id="formGroupNameInput" placeholder="Enter name of Beneficiary as in Aadhar Card">
      </div>


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

      <!--<div class="custom-file">
        <input type="file" class="custom-file-input" id="customFile" name="adhaaarcopy">
        <label class="custom-file-label" for="customFile">Choose file</label>
      </div>--> 
    </div>
    <!--logic for no-->
    <div ng-if="showHideTest=='false'">     
      <div class="form-group">
        <label for="formGroupDistrict">Please provide any of the following Identity Card*:</label>
        <select name="idcard" id="formGroupDistrict">
          <option value="1">Bank or Post Office photo passbook</option>
          <option value="2">Voter ID Card</option>
          <option value="3">Ration Card</option>
          <option value="4">Kishan Photo Passbook</option>
          <option value="5">Passport</option>
          <option value="6">Driving License</option>
          <option value="6">PAN Card</option>
          <option value="7">MGNREGS Job Card</option>
          <option value="8">Her husband's Employee Photo Identity Card issued by the government
           or any Public Sector Undertaking</option>
          <option value="9">Any other Photo Identity Card issued by State Government or Union Territory Administrations</option>
          <option value="10">Certificate of Identity with photograph issued by a Gazetted Officer on Official letterhead</option>
          <option value="11">Health Card issued by Primary Health Centre(PHC) or Government Hospital</option>
          <option value="12">Any other document specified by the State Government or Union Territory Adminstration</option>
        </select>
        </div>

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

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


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


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

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

       <!-- <div class="custom-file">

            <input type="file" class="custom-file-input" name="idcopy" id="customFile">
            <label class="custom-file-label" for="customFile">Choose file</label>
          </div>-->
    </div>
    </div>

   <!-- <div class="form-group form-check">
      <input name="checkbox" class="form-check-input" id="exampleCheck1" required>
      <label class="form-check-label" for="exampleCheck1">Check me out</label>
    </div>-->
    <button type="submit" class="btn btn-primary" style="margin-bottom:10px ">Submit</button>
  </form>
</div>   
{% endblock %}
</body>
```This is the html applyonline.html

2 个答案:

答案 0 :(得分:1)

idcard=request.POST.get('idcard',null=True)

这在Python中无效。如果您未指定默认值,则get()方法将返回None,因此您可以执行以下操作:

idcard=request.POST.get('idcard')

然后,如果要使其接受空值,则需要在模型字段中设置null=True

class Personal_Detail(models.Model):
    ...
    idno=models.IntegerField(blank=True, null=True)

blank=True表示表单将接受空值。

最后,如果您像这样更改模型,则必须先运行python manage.py makemigrations,然后运行python manage.py migrate才能创建迁移并将其应用于数据库。

答案 1 :(得分:0)

blank=True添加到CharField,不要忘记迁移更改。

beneficiary_adhaar_name=models.CharField(null=True,blank=True)