How to provide source data to django form wizard?

according to the documents of the form wizard, the source data must be a static dict. but you can dynamically provide raw data.

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 letter is what I want for the initial data in step 0, but I can only get this letter in the get_context_data method. How can i do this?

by the way, if urlconf for formwizard.as_view takes an argument like:

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

Dose means that I need to pass the variable to my form action attributes, because otherwise, when I submit the form, I will get an error of the found URL.

+4
source share
2 answers

You can override the get_form_initial method

 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 

Link: https://docs.djangoproject.com/en/dev/ref/contrib/formtools/form-wizard/#django.contrib.formtools.wizard.views.WizardView.get_form_initial

+5
source

first answer . You need to override get_form_initial , but self.kwargs (at least in the latest version of Django formtools) contains no GET or POST request parameters.

The solution is quite simple: just refer to the values ​​from the request directly, since self.request is an attribute directly on the wizard.

 def get_form_initial(self, step): initial = self.initial_dict.get(step, {}) invitation_key = self.request.GET.get("invitiation_key") email = InvitationKey.objects.get_key_email(invitation_key) initial.update({'email': email}) return initial 
+2
source

Source: https://habr.com/ru/post/1480133/


All Articles