dynamic - Django form wizard - choices depending on first form step -
i have created 2 step form formwizard follows:
- 1st step: asking user location
- 2nd step: show several search results depending on user location, , display radiobuttons
now second form depends on input of first form. several blogs or stackoverflow posts cover similar topics, , followed instructions. however, variable supposed saved during process_step not available next _init_.
how communicate variable location process_step _init_?
class remapstart(forms.form): location = forms.charfield() choices = [(x, x) x in ("cars", "bikes")] technology = forms.choicefield(choices=choices) class remaplocationconfirmation(forms.form): def __init__(self, user, *args, **kwargs): super(remaplocationconfirmation, self).__init__(*args, **kwargs) self.fields['locations'] = forms.choicefield(widget=radioselect(), choices=[(x, x) x in location]) class remapdata(forms.form): capacity = forms.integerfield() class remapwizard(formwizard): def process_step(self, request, form, step): if step == 1: self.extra_context['location'] = form.cleaned_data['location'] def done(self, request, form_list): # send email or save database, or whatever want # form parameters in form_list return httpresponseredirect('/contact/thanks/')
any absolutely appreciated.
thanks, h
ps: posting updated newer code.
i figured access post
dictionary directly in __init__
method because looks wizard passes post
each form instance via get_form
, don't see data reason.
instead of dwelling on long alternative i've come using render_template
hook.
class contactwizard(formwizard): def done(selef, request, form_list): return http.httpresponse([form.cleaned_data form in form_list]) def render_template(self, request, form, previous_fields, step, context=none): """ class using hidden fields pass state, manually grab location hidden fields (step-fieldname) """ if step == 2: locations = location.objects.filter(location=request.post.get('1-location')) form.fields['locations'].choices = [(x, x) x in locations] return super(contactwizard, self).render_template(request, form, previous_fields, step, context)
Comments
Post a Comment