首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >IntegrityError /personal_detail/

IntegrityError /personal_detail/
EN

Stack Overflow用户
提问于 2020-02-24 16:21:11
回答 2查看 49关注 0票数 0

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

代码语言:javascript
复制
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

代码语言:javascript
复制
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),但它不起作用)

代码语言:javascript
复制
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

我尝试在我的视图中使用nulll=False,比如idcard=request.POST.get('idcard',null=True),但是我再次遇到错误,get不能与null或类似的东西相关联。

applyonline.html

代码语言:javascript
复制
<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
代码语言:javascript
复制
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2020-02-24 16:29:34

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

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

票数 0
EN

Stack Overflow用户

发布于 2020-02-24 16:28:42

代码语言:javascript
复制
idcard=request.POST.get('idcard',null=True)

这在Python中无效。如果不指定默认值,则get()方法将返回None,因此在视图中可以这样做:

代码语言:javascript
复制
idcard=request.POST.get('idcard')

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

代码语言:javascript
复制
class Personal_Detail(models.Model):
    ...
    idno=models.IntegerField(blank=True, null=True)

blank=True意味着表单将接受空值。

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

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/60380036

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档