Django中的CharField ModelForm中的CheckboxSelectMultiple问题

问题描述

由于以下验证错误,我无法将表单保存在Dajngo中:

<ul class="errorlist"><li>pt_medical_condition<ul class="errorlist"><li>Select a valid choice.
 [&#x27;anx&#x27;,&#x27;Bip&#x27;] is not one of the available choices.
>pt_surgical_history<ul class="errorlist"><li>Select a valid choice. 
[&#x27;bre&#x27;,&#x27;pca&#x27;] is not one of the available choices.

我有这个模型:

class pt_data(models.Model):
   condition_choices = [('ane','Anemia'),('anx','Anxiety'),('art','Arthritis'),('ast','Asthma'),('Bip','Bipolar'),('ca','Cancer'),('clo','Clotting disorder'),('chf','CHF'),('mdd','Depression'),('cop','copD'),('ger','GERD'),('gla','Glaucome'),('hiv','HIV/AIDS'),('ibs','IBS/Crohn\'s'),('hld','High cholesterol'),('ckd','Kidney disease'),('ner','Nerve/Muscle disease'),('ocd','OCD'),('ost','Osteoporosis'),('pai','Pain disorder'),('pts','PTSD'),('sch','Schizophrenia'),('sei','Seizures'),('sca','Sickle cell anemia'),('su','Substance use disorder'),('thy','Thyroid disease')]

   surgery_choices = [('app','Appendix removal'),('bra','Brain surgery'),('bre','Breast surgery'),('cabg','CABG'),('pca','Cardiac stent'),('cho','gallbladder removal'),('col','Bowel surgery'),('csec','C-section'),('fra','Bone fracture repair'),('her','Hernia repair'),('hys','Uterus removal'),('joi','Joint replacement'),('lun','Lung surgery'),('spi','Spine/back surgery'),'Thyroid surgery'),('ton','Tonsil removal'),('strf','Tubal ligation'),('strm','Vasectomy'),('wei','Weight reduction surgery')]


   pt_medical_condition = models.CharField(max_length=100,blank=True,null=True,choices=condition_choices)
   pt_surgical_history = models.CharField(max_length=100,choices=surgery_choices)

这种形式:

class ptForm(forms.ModelForm):
   class Meta:
        model = pt_data
        fields = ('__all__')
        widgets = {
        'pt_medical_condition': CheckBoxSelectMultiple(attrs={'class': 'cond_checkBox'}),'pt_surgical_history': CheckBoxSelectMultiple(attrs={'class': 'surg_checkBox'}),}

这是我的HTML:

    {% for check in form.pt_medical_condition %}
        {% if forloop.counter|divisibleby:3 %}
            <div>            </div>
        {% endif %}
        <label class='mx-2' id="{{ check.choice_label }}">
            {{ check.tag }} {{ check.choice_label }}
        </label>
    {% endfor %}


    {% for check in form.pt_surgical_history %}
        {% if forloop.counter|divisibleby:2 %}
            <div>            </div>
        {% endif %}
        <label class='mx-2' id="{{ check.choice_label }}">
            {{ check.tag }} {{ check.choice_label }}
        </label>
    {% endfor %}

HTML呈现得很好,提交时似乎出现了问题。看起来那里是正确的字符,但是它们周围有错误的字符。以下“ anx”是适当的,但不会被Django验证,因为&#x27;anx&#x27上附加的所有其他内容。用AJAX发布信息会不会有问题?还是使用CSRF Coo​​kie进行发布?还有其他人遇到过这个问题吗?任何建议都会有所帮助。

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)