I'm trying to get django-formwizard to work with dynamically generated formset of the users' input in the first form.
The forms render and initial data shows up fine, but when I try to submit the 2nd form, I get it back without errors and I do not proceed to the done()
method.
My forms:
class MenuInputForm(forms.Form):
content = forms.CharField()
class DayMenuForm(forms.Form):
menu = forms.CharField()
DayMenuFormSet = formset_factory(form=DayMenuForm, min_num=1, extra=0)
My views:
class MenuAddWizard(LoginRequiredMixin, SessionWizardView):
# Either provide form_list as part of .as_view() in urls, or here.
# Note: DayMenuFormSet is overwritten in get_form()
form_list = [MenuInputForm, DayMenuFormSet]
template_name = "menu_add.html"
def get_form(self, step=None, data=None, files=None):
form = super().get_form(step, data, files)
# Determine the step if not given
if step is None:
step = self.steps.current
if step == '1':
step_0_cleaned_data = self.get_cleaned_data_for_step('0')
# Using step 0 data, I generate the list to be used for initial; removing logic for brevity and using two example items as test
DayMenuFormSet = formset_factory(form=DayMenuForm, extra=0)
formset = DayMenuFormSet(initial=[{'menu': 'Some example menu'}, {'menu': 'This is some other example text'}])
form = formset
return form
def done(self, form_list, **kwargs):
# This code is never reached, whatever I put here (redirect, print, ...)
pass
Template:
<form action="" method="post">{% csrf_token %}
<table>
{{ wizard.management_form }}
{% if wizard.form.forms %}
{{ wizard.form.management_form }}
{% for form in wizard.form.forms %}
{{ form.as_table }}
{% endfor %}
{% else %}
{{ wizard.form }}
{% endif %}
</table>
<input type="submit" value="Submit"/>
</form>
What am I doing wrong or forgetting about?
Could it simply be that .done()
returns nothing? Normally, you should render a template or even better, redirect to a view.
Update:
I now see you're not passing any data to the formset in get_form()
. Therefore, the form is never validated, and no errors are shown because the form thinks no data was submitted.
So change this line
formset = DayMenuFormSet(initial=[{'menu': 'Some example menu'}, {'menu': 'This is some other example text'}])
to
formset = DayMenuFormSet(
initial=[{'menu': 'Some example menu'}, {'menu': 'This is some other example text'}],
data=data # <- You forgot about this.
)