I am new to Django.
Now I have class defined below
PROPERTY_TYPE_CHOICE = [['1', 'Fixed (TODO)'],
['2', 'Trajectory (TODO)'],
['3', 'Error_Detecting'],
['4', 'Error_Correcting']]
FIXED_TYPE_CHOICE = [['1', 'Prefix',
'2', 'Suffix']]
class UploadFileForm(forms.Form):
# title = forms.CharField(max_length=50)
automata_file = forms.FileField(required = True)
transducer_file = forms.FileField(required = True)
property_type = forms.ChoiceField(choices=PROPERTY_TYPE_CHOICE,
required=True)
fixed_type = forms.ChoiceField(choices=FIXED_TYPE_CHOICE,
required=True)
debug_output = forms.BooleanField(required=False)
I have this PROPERTY_TYPE_CHOICE showed in the front html
<div class="fieldWrapper">
{{ form.property_type.errors }}
<label for="id_a">Select <u>a type</u> of property:</label>
{{ form.property_type }}
</div>
and I want to show the FIXED_TYPE_CHOICE if I choose the first choice “Fixed (TODO)” in the PROPERTY_TYPE_CHOICE.
I read the docs about Django, and I think it may be implemented in this way:
<div class="fieldWrapper">
{{ form.property_type.errors }}
<label for="id_a">Select <u>a type</u> of property:</label>
{{ form.property_type }}
</div>
{% if form.property_type=='1' %}
<div class="fieldWrapper">
{{ form.fixed_type.errors }}
<label for="id_a">Select <u>a fixed type</u> of property:</label>
{ { form.fixed_type }}
</div>
{% endif %}
But I can’t do that.
What should I do? Thank you.
form.property_typeis a BoundForm object, if you want to access its value you’ll need to useform.property_type.valueWhich is why your comparison of
{% if form.property_type=='1'}doesn’t work.For more details see this page about 2/3rds of the way down, under the Customizing the Form Template section.
For updating without a page refresh, you’ll need Javascript like the following (make sure you also include the jQuery library (http://jquery.com/):
And make sure that the give the dropdowns corresponding id attributes (
id="property_type") for example.