Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to provide initial data to django form wizard?

According to the form wizard docs, the initial data should be a static dict. But is it possible to provide initial data dynamically?

Here is my situation:

 def get_context_data(self, form, **kwargs):
    context = super(debugRegistrationWizard, self).get_context_data(form=form, **kwargs)
    email = InvitationKey.objects.get_key_email(self.kwargs['invitation_key'])
    context.update({'invitation_key': self.kwargs['invitation_key']})
    return context

The email is what I want for initial data in step0, but I can only get this email in get_context_data method. How can I do that?

By the way: if the urlconf for formwizard.as_view accept argument like:

url(r'^registration/(?P<invitation_key>\w+)$', debugRegistrationWizard.as_view(FORMS)),

does it mean I have to pass a variable to my form's action attributes, because otherwise when I submit the form, I will get a not found url error.

like image 349
paynestrike Avatar asked Jan 13 '23 10:01

paynestrike


1 Answers

You can override the method get_form_initial

def get_form_initial(self, step):
    initial = self.initial_dict.get(step, {})
    if step == 42:
        email = InvitationKey.objects.get_key_email(self.kwargs['invitation_key'])
        initial.update({'email': email})
    return initial

Ref: https://django-formtools.readthedocs.io/en/latest/wizard.html#formtools.wizard.views.WizardView.get_form_initial

like image 87
tuxcanfly Avatar answered Jan 20 '23 07:01

tuxcanfly