(仍然未回答)Django复选框在数据库中保存是或否

8 javascript django

  <form id="form" name="form">
    <input type="checkbox" value="1" name="Asthma" data-form-field="Option" class="form-check-input display-7" id="checkbox1"  title="Check if Student have Asthma">
    <input type="checkbox" value="1" name="Congenital" data-form-field="Option" class="form-check-input display-7" id="checkbox1" title="Check if Student have Congenital Anomalies">
    <input type="checkbox" value="1" name="Contact" data-form-field="Option" class="form-check-input display-7" id="checkbox1" title="Check if Student use Contact lens">
  </form>
Run Code Online (Sandbox Code Playgroud)

我的HTML中有此代码

<script type="text/javascript">
                        // when page is ready
                        $(document).ready(function() {
                             // on form submit
                            $("#form").on('submit', function() {
                                // to each unchecked checkbox
                                $(this + 'input[type=checkbox]:not(:checked)').each(function () {
                                    // set value 0 and check it
                                    $(this).attr('checked', true).val(0);
                                });
                            })
                        })
                    </script>
Run Code Online (Sandbox Code Playgroud)

我的问题是,每次我将结果始终自动保存到数据库时,即使我未选中html中的checkbox(NO)。我不知道我在用JavaScript做的是否正确

这是我的views.py

    Asthma = request.POST['Asthma']
    Congenital = request.POST['Congenital']
    Contact = request.POST['Contact']
V_insert_data = StudentUserMedicalRecord( 
        Asthma=Asthma,
        CongenitalAnomalies=Congenital,
        ContactLenses=Contact
   )
 V_insert_data.save()
Run Code Online (Sandbox Code Playgroud)

models.py

Asthma=models.BooleanField(null=True, blank=True, default=False)
CongenitalAnomalies=models.BooleanField(null=True,blank=True, default=False)
ContactLenses=models.BooleanField(null=True,blank=True, default=False)
Run Code Online (Sandbox Code Playgroud)

html.py 在此处输入图片说明

即使我从我的html中插入记录未选中(否),结果在我的数据库中也总是自动“是”,您能解决我的JavaScript代码吗?似乎不起作用。

在此处输入图片说明

san*_*eep 1

删除所有 JavaScript。这就是将所有 0 转换为 1。在views.py中使用它

Asthma = request.POST.get('Asthma', 0)  # 0  not '0'
Congenital = request.POST.get('Congenital', 0)  # 0  not '0'
Contact = request.POST.get('Contact', 0)  # 0  not '0'
Run Code Online (Sandbox Code Playgroud)

而不是这个:

Asthma = request.POST['Asthma']
Congenital = request.POST['Congenital']
Contact = request.POST['Contact']
Run Code Online (Sandbox Code Playgroud)

- - - - - - -编辑 - - - - - - - - -

意见

Asthma = request.POST.get('Asthma', 0) == '1'
Congenital = request.POST.get('Congenital', 0) == '1'
Contact = request.POST.get('Contact', 0) == '1'
V_insert_data = StudentUserMedicalRecord(
            Asthma=Asthma,
            CongenitalAnomalies=Congenital,
            ContactLenses=Contact
)
V_insert_data.save()
Run Code Online (Sandbox Code Playgroud)

模板

<input type="checkbox" value="1" name="Asthma" data-form-field="Option" class="form-check-input display-7" id="checkbox1"  title="Check if Student have Asthma">
<input type="checkbox" value="1" name="Congenital" data-form-field="Option" class="form-check-input display-7" id="checkbox2" title="Check if Student have Congenital Anomalies">
<input type="checkbox" value="1" name="Contact" data-form-field="Option" class="form-check-input display-7" id="checkbox3" title="Check if Student use Contact lens">
Run Code Online (Sandbox Code Playgroud)