Sunday, May 29, 2011

Re: Django dynamic form simple Example

I do not know the details of implementing this, however I do know that the approach you are trying to take is incorrect.  It is not generally a good idea to dynamically change the definition of a form like that.  What I suggest is that you use formsets to display multiple instances of the same form.  So for example, if you changed your form definition to:

class testform(forms.Form):
    Q = forms.CharField()

You could then create a formset like so:
from django.forms.formsets import formset_factory
formset = formset_factory(testform, extra=2)
Once displayed in the template, this would show two instances of the form giving you the two fields you require.  You could then either change the extra attribute in the view if you know how many fields you require, or you could use the django-dynamic-formset jQuery plugin in order to allow the user to add or remove more forms dynamically.

Hope this helps,

Ryan

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To post to this group, send email to django-users@googlegroups.com.
To unsubscribe from this group, send email to django-users+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.

No comments:

Post a Comment