如何解决Django中的CharField ModelForm中的CheckboxSelectMultiple问题
由于以下验证错误,我无法将表单保存在Dajngo中:
<ul class="errorlist"><li>pt_medical_condition<ul class="errorlist"><li>Select a valid choice.
['anx','Bip'] is not one of the available choices.
>pt_surgical_history<ul class="errorlist"><li>Select a valid choice.
['bre','pca'] 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验证,因为'anx'
上附加的所有其他内容。用AJAX发布信息会不会有问题?还是使用CSRF Cookie进行发布?还有其他人遇到过这个问题吗?任何建议都会有所帮助。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。