[Fixed]-Dynamic form complains about number of "steps" as an unexpected keyword argument

1👍

Are you forgetting to pop step_number from the kwargs before you call TaskForm’s super init?

You should pop from the kwargs rather than get, otherwise the form will throw an exception like you’re seeing because the super().__init__ does not expect the extra param.

class TaskForm(forms.Form):
    task_name = forms.CharField()
    step_number = forms.IntegerField(widget=forms.HiddenInput())

    def __init__(self, *args, **kwargs):
        step_fields = kwargs.pop('step_number', 0)
        super(TaskForm, self).__init__(*args, **kwargs)
        self.fields['step_number'].initial = step_fields

        for index in range(int(step_fields)):
            self.fields['step_field_{index}'.format(index=index)] = forms.CharField()

Leave a comment