[Answered ]-Change form dynamically in django


I would use a base form class, and have two different forms inherit from the base one. You can load a different form via ajax depending on user input. You can probably even use the same template for the form. Your view would have something like




based on what ‘method’ is. forms.py would change to something like this:

_a_choices = (
("a", "A"),
("b", "B"),
("c", "C"),

_d_choices = (
("d", "D"),
("e", "E"),

class simpleForm(forms.Form):
    # this is the first form
    # all other fields here
    simple_variable = forms.DecimalField()

class simpleFormA(simpleForm):
    chosen_method = forms.ChoiceField(label="Método", choices=_a_choices)

class simpleFormB(simpleForm):
    chosen_method = forms.ChoiceField(label="Método", choices=_b_choices)

Make sense?


Leave a comment