I'm trying to understand how to write a multiwidget for a form field in Django. I have a checkbox field and then a CharField which I would like to enable/disable with Javascript based on whether the checkbox field has been selected, so am trying to add an 'id' to both fields.
I understand that this requires a multiwidget but I'm struggling to understand how to go about this.
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))
Just like the 'gender_select' field, I need to add the following to 'gender_priority':
forms.TextInput(attrs={'id':'gender_priority'})
but as far as I understand, I can only include one widget. How would I go about including more than one?
I eventually solved this, for anyone looking for an answer:
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;
};
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.