In Django, how can I link an unrelated form?

I generate the form dynamically:

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

and then display 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 the linked instance by changing the first line to

 form = forms.Form(request.POST) 

before I start generating a dynamic form.

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

thanks

+4
source share
1 answer

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

Try using:

 form.data = request.POST.copy() form.is_bound = True 
+5
source

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


All Articles