4

I generate a form dynamically:

form = forms.Form()
form.fields['myname'] = forms.CharField(label=u'My Name')
...

and then show the form with:

buf = '....<form action="." method="POST">...' + form.as_p() + '...'
t = Template(buf)
v = RequestContext(request, {'form': form})
html = t.render(v)
...

I could get a bound instance, by changing the first line to

form = forms.Form(request.POST)

before I begin to generate the dynamic form.

However, is there a way to keep the dynamic form generation code as is, and then late bind the form to request.POST data?

Thanks

Raja
  • 41
  • 3

1 Answers1

5

Looking in django/forms/forms.py, you can see how django works. It seems that form data is saved to form.data. Also, if data is not None, then form.is_bound is set to true.

Try using:

form.data = request.POST.copy()
form.is_bound = True
Philip Ramirez
  • 2,972
  • 1
  • 16
  • 16