(仍然未回答)Django复选框在数据库中保存是或否 [英] (still unAnswered) django checkbox save yes or no in the database

查看:60
本文介绍了(仍然未回答)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>

我的html中有此代码

i have this code in my 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>

我的问题是,每次我将结果保存到数据库中时,结果总是自动为是",即使我未选中html中的checkbox(NO)也是如此.我不知道我的JavaScript是否正确

my problem is everytime i save into my database the result always automatic YES, even i unchecked the checkbox(NO) in the html. i dont know if i am doing right in my javascript

这是我的views.py

this is my 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()

models.py

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)

html.py

即使我从我的html中插入记录也未选中(否),结果在我的数据库中始终自动为是",您能解决我的javascript代码吗?似乎不起作用.

even i insert record from my html is unchecked(No), the result always automatic "Yes" in my database, can you please fixed my javascript code? seems like its not working.

推荐答案

删除所有JavaScript.那就是将所有的0转换为1.在 views.py

Remove all javascript. That's converting all 0s to 1. Use this in 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'

代替此:

Asthma = request.POST['Asthma']
Congenital = request.POST['Congenital']
Contact = request.POST['Contact']

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

观看次数

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()

模板

<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">

这篇关于(仍然未回答)Django复选框在数据库中保存是或否的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆