用于CharField表单字段的Django multiwidget

时间:2019-08-08 19:01:09

标签: python django

我试图了解如何在Django中为表单字段编写一个multiwidget。我有一个复选框字段,然后是一个CharField,我想根据是否已选中该复选框字段来使用Javascript启用/禁用它,因此我试图在两个字段中添加一个“ id”。

我知道这需要多种工具,但是我正在努力了解如何解决这个问题。

from django import forms

PRIORITY_CHOICES = [('high', 'High'), ('low', 'Low')]

class UploadForm(forms.Form):
gender_select = forms.BooleanField(label='Even Gender Distribution', required=False, widget=forms.TextInput(attrs={'id':'gender_select'}))
gender_priority = forms.CharField(label='Select Priority', required=False, widget=forms.Select(choices=PRIORITY_CHOICES))

就像“ gender_select”字段一样,我需要在“ gender_priority”中添加以下内容:

forms.TextInput(attrs={'id':'gender_priority'})

但是据我了解,我只能包含一个小部件。我将如何包括多个?

1 个答案:

答案 0 :(得分:0)

对于寻求答案的任何人,我最终都解决了这个问题:

PRIORITY_CHOICES = [('high', 'High'), ('low', 'Low')]

class UploadForm(forms.Form):   
    gender_select = forms.BooleanField(label='Even Gender Distribution', required=False, widget=forms.CheckboxInput(attrs={'id':'gender_select'}))
    gender_priority = forms.ChoiceField(label='Priority', required=False, choices=PRIORITY_CHOICES, widget=forms.Select(attrs={'id':'gender_priority'}), disabled=True)

js:

document.getElementById('academic_score_select').onchange = function() {
    document.getElementById('academic_score_priority').disabled = !this.checked;

};