how to set field value of a form object before render it?

1,235 views
Skip to first unread message

hao he

unread,
Aug 28, 2009, 10:35:24 PM8/28/09
to django...@googlegroups.com
hi.

this following code show what I want to do,but 'red' line is incorrect,

 if request.method == "POST":
      form = MyForm(request.POST)
      c = form.data["a"] + form.data["b"]
      form.data["c"] = c
      return render_to_response(template_name , {"form": form})

how to modify it? thanks.
       


Karen Tracey

unread,
Aug 28, 2009, 11:03:37 PM8/28/09
to django...@googlegroups.com

Perhaps if you put a few more words around what it is you are trying to do someone might be able to offer useful help.  This code looks quite odd to me.  You generally don't want to take values from form.data (which will not have been normalized) but rather validate the form and then take values from form.cleaned_data (which will be both validated and normalized).   Doing nothing with what you've got other than displaying the form (again?) also seems a bit odd.

Karen

hao he

unread,
Aug 29, 2009, 5:44:36 AM8/29/09
to django...@googlegroups.com
thanks for your reply。

yes,i want to display the form again with some field being modified.

how to set the new value to a field of fhe old form to render ?

for example :
in site signup case.
when there are some errors, we hope to display the signup again ,but the password field would been cleared up。
how to clear it ?

thanks 。



2009/8/29 Karen Tracey <kmtr...@gmail.com>

Karen Tracey

unread,
Aug 29, 2009, 8:50:26 AM8/29/09
to django...@googlegroups.com
2009/8/29 hao he <spee...@gmail.com>
thanks for your reply。

yes,i want to display the form again with some field being modified.

how to set the new value to a field of fhe old form to render ?

for example :
in site signup case.
when there are some errors, we hope to display the signup again ,but the password field would been cleared up。
how to clear it

For the particular case of a password that you want the user to re-enter upon form re-submission, use the render_value argument to the PasswordInput widget:

http://docs.djangoproject.com/en/dev/ref/forms/widgets/#django.forms.PasswordInput

It isn't clear to me if you have a more general requirement than that?
Karen

conrad

unread,
Aug 29, 2009, 10:26:07 AM8/29/09
to Django users
I believe you can do the following:

if request.method == "POST":
form = MyForm(request.POST)
c = form.data["a"] + form.data["b"]
form.fields["c"].initial = c
return render_to_response(template_name , {"form": form})

assuming the initial attribute makes sense for the 'c' field. You can
can dynamically reset field attributes (and even dynamically add
fields) to a form.

conrad


On Aug 29, 5:50 am, Karen Tracey <kmtra...@gmail.com> wrote:
> 2009/8/29 hao he <speed...@gmail.com>
>
> > thanks for your reply。
>
> > yes,i want to display the form again with some field being modified.
>
> > how to set the new value to a field of fhe old form to render ?
>
> > for example :
> > in site signup case.
> > when there are some errors, we hope to display the signup again ,but the
> > password field would been cleared up。
> > how to clear it
>
> For the particular case of a password that you want the user to re-enter
> upon form re-submission, use the render_value argument to the PasswordInput
> widget:
>
> http://docs.djangoproject.com/en/dev/ref/forms/widgets/#django.forms....
Reply all
Reply to author
Forward
0 new messages