I need to get the selected Value of a forms.Choicefield
for an if/else statement which produces another forms.Choicefield
based on the selected Value
forms.py:
class ReceiverForm(forms.Form):
receivers = forms.ChoiceField(choices=db_mails(), required=True, label='Receivers')
if db_certs(<- selected value from receivers ChoiceField ->):
print "cert found"
encryption = forms.ChoiceField(choices=EncryptionChoiceAll, initial='smime_mail', required=True, label='Encryption')
else:
print "no cert found"
encryption = forms.ChoiceField(choices=EncryptionChoiceNoCert, initial='smime_mail', required=True, label='Encryption')
db_mails()
and db_certs(mail)
are working as expected
Is there a way to achieve what i need in forms.py or am I totally wrong with the design?
When the form is built and you specify the choice list, you have no way to know which value is selected, as:
So at the time you create the class you do not have the information.
There are possibilities to have dynamic values in one choice, depending on the other one, but this needs to be done on the client side, when the user changes the selection:
There are plenty of tutorials to do this, and this is not directly linked to Django. As an example: