Thursday, August 29, 2013

Django Form Wizard: Why is this happening?

I have a dinamic form wizard step as following:

class AltaForm6(forms.Form):  CHOICES = ((1, 1,), (2, 2,))    username = ChoiceFieldInvalid(required=True, label="Usuario", widget=forms.RadioSelect, choices=CHOICES)  username2 = forms.CharField(required=False, label="Otro")    def clean_username2(self):    username = self.cleaned_data.get('username')    username2 = self.cleaned_data.get('username2')      if username == "Otro" and len(username2) == 0:      raise forms.ValidationError("Debe especificar un nombre de usuario")    return username2    def clean_username(self):    username = self.cleaned_data.get('username')    return username

Then i dynamically change the choices values:

class AltaWizard(SessionWizardView):    template_name = 'agroshare/wizard.html'    def get_form(self, step=None, data=None, files=None):      form = super(AltaWizard, self).get_form(step, data, files)      if step == '5':           import logging         logger = logging.getLogger(__name__)           cleaned_data = self.get_cleaned_data_for_step('2') or {}         logger.error(cleaned_data)         nombre = cleaned_data.get('nombre')         apellido = cleaned_data.get('apellido')           first = possibleUid(nombre, apellido, '1')         second = possibleUid(nombre, apellido, '2')           if ' ' in nombre:            third = possibleUid(nombre, apellido, '3')            form.fields['username'].choices = ((first, first,), (second, second,), (third, third,), ("Otro", "Otro",))         if ' ' in apellido:            fourth = possibleUid(nombre, apellido, '4')            form.fields['username'].choices = ((first, first,), (second, second,), (fourth, fourth,), ("Otro", "Otro",))         else:            form.fields['username'].choices = ((first, first,), (second, second,), ("Otro", "Otro",))      if step == '5':         form.user = self.request.user      return form        def render(self, form=None, **kwargs):      form = form or self.get_form()      context = self.get_context_data(form=form, **kwargs)      if self.steps.current == '5':         form2 = self.get_form('5')         cleaned_data = self.get_cleaned_data_for_step('5') or {}         username = cleaned_data.get('username')         form2.fields['username'].choices = [username]      return self.render_to_response(context)

The problem is, when i go back to this step trough the wizard, it does not modify the "choices" values, it shows "CHOICES = ((1, 1,), (2, 2,))"

How can i achieve that when i go back to this step, the form can actually show the values i want?



--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
For more options, visit https://groups.google.com/groups/opt_out.

No comments:

Post a Comment