Pre-filling form data without submission?

1,873 views
Skip to first unread message

shogu...@googlemail.com

unread,
Jan 20, 2009, 7:12:13 AM1/20/09
to Django users
Hi guys,

I've hunted around the docs and googled this (it's quite a niche
scenario so a bit hard to find!) and I'm still a bit stuck! I was
hoping somebody might be able to help out!

Can anyone tell me how to pre-fill form data without the form
automatically calling submit on the form so that the validation
messages don't appear in the rendered template?

Here's the view code:

..
if request.POST:
form = MyForm(request.POST)
if form.is_valid():
do_stuff()
else:
form = MyForm({'field_1': field_data_1, 'field_2':
field_data_2, ... })
return ...
..

Basically I want to pre-populate MyForm with the data listed in the
dictionary above, but it always calls the submit method when I load
the template! I'm stuck... :-)

Thanks for any help you can provide,

Mike.

Thomas Guettler

unread,
Jan 20, 2009, 7:33:36 AM1/20/09
to django...@googlegroups.com
shogu...@googlemail.com schrieb:

> Hi guys,
>
> I've hunted around the docs and googled this (it's quite a niche
> scenario so a bit hard to find!) and I'm still a bit stuck! I was
> hoping somebody might be able to help out!
>
> Can anyone tell me how to pre-fill form data without the form
> automatically calling submit on the form so that the validation
> messages don't appear in the rendered template?
>
>
>
Hi,

Check the docs for "initial".

Thomas

--
Thomas Guettler, http://www.thomas-guettler.de/
E-Mail: guettli (*) thomas-guettler + de

shogu...@googlemail.com

unread,
Jan 20, 2009, 7:56:08 AM1/20/09
to Django users
Hi Thomas,

Thanks for your quick reply!

I had a look at the docs, but it seems I have to enter the initial
values at the Form definition level. However, I am interested in
entering dynamic data depending on who is viewing the form. For
instance, in my case I want the user's email address to be displayed
in the form when they view the page. Since the form definition doesn't
have access to the request object, I presume this is not possible.

Is there any other way to do this?

Thanks,

Mike.

On Jan 20, 12:33 pm, Thomas Guettler <h...@tbz-pariv.de> wrote:
> shogunm...@googlemail.com schrieb:> Hi guys,
>
> > I've hunted around the docs and googled this (it's quite a niche
> > scenario so a bit hard to find!) and I'm still a bit stuck! I was
> > hoping somebody might be able to help out!
>
> > Can anyone tell me how to pre-fill form data without the form
> > automatically calling submit on the form so that the validation
> > messages don't appear in the rendered template?
>
> Hi,
>
> Check the docs for "initial".
>
>   Thomas
>
> --
> Thomas Guettler,http://www.thomas-guettler.de/

Thomas Guettler

unread,
Jan 20, 2009, 8:42:03 AM1/20/09
to django...@googlegroups.com
Hi,

at python level you can do it like this (untested):

def myview(request, ...):
form=MyForm(..., initial={'email': request.user.email})

or

form.fields['email'].initial=request.user.email


shogu...@googlemail.com schrieb:


> Hi Thomas,
>
> Thanks for your quick reply!
>
> I had a look at the docs, but it seems I have to enter the initial
> values at the Form definition level. However, I am interested in
> entering dynamic data depending on who is viewing the form. For
> instance, in my case I want the user's email address to be displayed
> in the form when they view the page. Since the form definition doesn't
> have access to the request object, I presume this is not possible.
>
>


--
Thomas Guettler, http://www.thomas-guettler.de/

Daniel Roseman

unread,
Jan 20, 2009, 8:53:44 AM1/20/09
to Django users
On Jan 20, 12:56 pm, "shogunm...@googlemail.com"
<shogunm...@googlemail.com> wrote:
> Hi Thomas,
>
> Thanks for your quick reply!
>
> I had a look at the docs, but it seems I have to enter the initial
> values at the Form definition level. However, I am interested in
> entering dynamic data depending on who is viewing the form. For
> instance, in my case I want the user's email address to be displayed
> in the form when they view the page. Since the form definition doesn't
> have access to the request object, I presume this is not possible.
>
> Is there any other way to do this?
>
> Thanks,
>
> Mike.

No, you can pass initial data when you instantiate the form in your
view.

f = MyFormClass(initial=initial_data)

See here: http://docs.djangoproject.com/en/dev/ref/forms/fields/#dynamic-initial-values
I have to admit it took some searching before I could find this link,
even though I knew it was there; the 'fields' page doesn't seem like a
very obvious place for it.

--
DR.

shogu...@googlemail.com

unread,
Jan 20, 2009, 9:17:02 AM1/20/09
to Django users
Thanks guys, I'm gonna give this a try!

Mike.

On Jan 20, 1:53 pm, Daniel Roseman <roseman.dan...@googlemail.com>
wrote:
> On Jan 20, 12:56 pm, "shogunm...@googlemail.com"
>
>
>
> <shogunm...@googlemail.com> wrote:
> > Hi Thomas,
>
> > Thanks for your quick reply!
>
> > I had a look at the docs, but it seems I have to enter the initial
> > values at the Form definition level. However, I am interested in
> > entering dynamic data depending on who is viewing the form. For
> > instance, in my case I want the user's email address to be displayed
> > in the form when they view the page. Since the form definition doesn't
> > have access to the request object, I presume this is not possible.
>
> > Is there any other way to do this?
>
> > Thanks,
>
> > Mike.
>
> No, you can pass initial data when you instantiate the form in your
> view.
>
> f = MyFormClass(initial=initial_data)
>
> See here:http://docs.djangoproject.com/en/dev/ref/forms/fields/#dynamic-initia...

Facundo Casco

unread,
Apr 8, 2009, 11:10:07 AM4/8/09
to django...@googlegroups.com
On Tue, Jan 20, 2009 at 11:17 AM, shogu...@googlemail.com
<shogu...@googlemail.com> wrote:
>
> Thanks guys, I'm gonna give this a try!
>
> Mike.
Hi, I'm having the exact same problem you had and I can't find a nice
way around it.
I want the name of the currently logged in user to be pre-selected in
a combo box in the form.

The form is used to create a new object in the database. What I've
found as the main problem is that when I display the form for the
first time, if it's bound it throws validation errors (because I don't
want to pre-populate all the required fields) and if it is not bound
it just doesn't display the pre-populated data.

My workaround at the time is to pass a flag to the template so it
doesn't show the errors but I still don't think that is the best way
to do it.

Any help would be appreciated.

Adi Sieker

unread,
Apr 8, 2009, 11:28:02 AM4/8/09
to django...@googlegroups.com

If you create tHe Form ala

form = Form( initial={'user': request.user})

it should prepopulate the field and not throw validation errors.

if on the other hand you create the form like:

form = Form({'user': request.user})

you bind the form and it validates on output.

adi

Facundo Casco

unread,
Apr 8, 2009, 12:02:09 PM4/8/09
to django...@googlegroups.com
On Wed, Apr 8, 2009 at 12:28 PM, Adi Sieker <a...@sieker.info> wrote:
>
> If you create tHe Form ala
>
> form = Form( initial={'user': request.user})
>
> it should prepopulate the field and not throw validation errors.
>
> if on the other hand you create the form like:
>
> form = Form({'user': request.user})
>
> you bind the form and it validates on output.
>
> adi
>
Yeap, sorry, my bad.
I've just noted that.
Thanks.

Hernan Olivera

unread,
Apr 8, 2009, 12:37:07 PM4/8/09
to django...@googlegroups.com
My (working code)solution:

form = CargaRecetaForm(initial={'obra_social':str(os.siglaosocial),
'plan':str(plan.descplan), 'CU': str(numfar), 'NrodeOrden':
str(sigord)})

contexto = RequestContext(request, {'form':form})

return render_to_response('carga_page2.html', contexto)
--
Hernan Olivera

Reply all
Reply to author
Forward
0 new messages