how to create a hidden field

1,372 views
Skip to first unread message

webcomm

unread,
Jul 25, 2011, 9:18:29 PM7/25/11
to Django users
I haven't been able to find anything documenting how to create a
hidden field. I see this is done with the HiddenField widget but
don't know how to use that. I tried the following but the field still
shows...

class NewsForm(ModelForm):
itemid =
forms.IntegerField(widget=forms.HiddenInput,required=False)

class Meta:
model = News


Thanks,
Ryan

Tom Evans

unread,
Jul 26, 2011, 5:40:08 AM7/26/11
to django...@googlegroups.com

That would hide the field called 'itemid' from the model 'News'. Are
you saying it doesn't?

Cheers

Tom

webcomm

unread,
Aug 2, 2011, 9:37:09 AM8/2/11
to Django users
> That would hide the field called 'itemid' from the model 'News'. Are
> you saying it doesn't?

That's right. It's not working. The field isn't hidden.

-Ryan

Tom Evans

unread,
Aug 2, 2011, 9:50:51 AM8/2/11
to django...@googlegroups.com

Have you simplified your example? This functionality works well when
used correctly:

>>> class TestForm(ModelForm):
... range = CharField(widget=HiddenInput)
... class Meta:
... model=Foo
...
>>> f=TestForm()
>>> f.as_p()
u'<p><label for="id_name">Name:</label> <input type="text" name="name"
id="id_name" /><input type="hidden" name="range" id="id_range" /></p>'

As you can see, the range field is clearly hidden.

Cheers

Tom

webcomm

unread,
Aug 2, 2011, 10:47:17 AM8/2/11
to Django users
> Have you simplified your example?

The example I provided in the original post is the actual code in my
models.py. It's not simplified.

> >>> class TestForm(ModelForm):
>
> ...   range = CharField(widget=HiddenInput)
> ...   class Meta:
> ...     model=Foo
> ...>>> f=TestForm()
> >>> f.as_p()
>
> u'<p><label for="id_name">Name:</label> <input type="text" name="name"
> id="id_name" /><input type="hidden" name="range" id="id_range" /></p>'
>
> As you can see, the range field is clearly hidden.

I get "CharField is not defined" when I try your code. I find that I
have to do forms.SomeField. Maybe we're importing different things.
In my models.py I have...

from django.db import models
from django import forms
from django.forms import ModelForm

I'm using django 1.3.

-Ryan

Tom Evans

unread,
Aug 2, 2011, 12:24:05 PM8/2/11
to django...@googlegroups.com

Well, in the console I cannot be arsed to continually type
forms.SomeThing, so I imported * from django.forms, but this is
largely irrelevant.

You still haven't shown that it doesn't hide the field, eg by posting
the output of frm.as_p(), where as I have shown conclusively that it
does, so I think something else is going on here. Are you sure the
field 'itemid' exists on your model?

This is speculation, but I think you don't have a field called itemid
on your model, and so are creating an additional field called itemid,
which is hidden. You display the form, you don't see the new, hidden,
'itemid' field because it is hidden, but you do see your field which
is not called 'itemid', and you then think that the field is not
hidden.

It's not hard to test this, go to your django console, import the
form, instantiate it and call the as_p method. Do you have a hidden
field called 'itemid' or not?

Cheers

Tom

webcomm

unread,
Aug 2, 2011, 4:14:39 PM8/2/11
to Django users
> You still haven't shown that it doesn't hide the field, eg by posting
> the output of frm.as_p(), where as I have shown conclusively that it
> does, so I think something else is going on here. Are you sure the
> field 'itemid' exists on your model?

Thanks Tom. The field itemid does exist in the model. I am getting a
different result when I use as_p() interactively than what I get non-
interactively.

With the call to as_p I get the desired result, like you did, wherein
the field is hidden:

u'<p><label for="id_title">Title:</label> <input id="id_title"
type="text" name="title" maxlength="255" /></p>\n<p><label
for="id_summary">Summary:</label> <textarea id="id_summary" rows="10"
cols="40" name="summary"></textarea></p>\n<p><label
for="id_body">Body:</label> <textarea id="id_body" rows="10" cols="40"
name="body"></textarea></p>\n<p><label for="id_category">Category:</
label> <select name="category" id="id_category">\n<option value=""
selected="selected">---------</option>\n<option value="1">Old News</
option>\n<option value="2">Breaking</option>\n</select></p>\n<p><label
for="id_author">Author:</label> <select multiple="multiple"
name="author" id="id_author">\n<option value="1">Ty Cobb</option>
\n<option value="2">Babe Ruth</option>\n<option value="3">Mike
Schmidt</option>\n</select> <span class="helptext"> Hold down
"Control", or "Command" on a Mac, to select more than one.</span></p>
\n<p><label for="id_path">Path:</label> <input id="id_path"
type="text" name="path" maxlength="255" /><input type="hidden"
name="itemid" id="id_itemid" /></p>'

Non-interactively, in the automatic admin, what I get is a visible
text input below the title field and above the summary field. Here's
the relevant model and modelform: http://pastebin.com/azKgdraw

-Ryan

Brian Neal

unread,
Aug 2, 2011, 4:56:53 PM8/2/11
to Django users
On Aug 2, 3:14 pm, webcomm <rya...@gmail.com> wrote:
> ...
> Non-interactively, in the automatic admin, what I get is a visible
> text input below the title field and above the summary field.  Here's
> the relevant model and modelform:http://pastebin.com/azKgdraw
>
> -Ryan

Oh...you are trying to do this in the *admin*? Are you sure you just
don't need to "exclude" the form field?

https://docs.djangoproject.com/en/1.3/ref/contrib/admin/#django.contrib.admin.ModelAdmin.exclude

If you really want the admin to use your custom form you have to tell
it to:

https://docs.djangoproject.com/en/1.3/ref/contrib/admin/#django.contrib.admin.ModelAdmin.form

Best,
BN

webcomm

unread,
Aug 2, 2011, 5:08:14 PM8/2/11
to Django users
On Aug 2, 4:56 pm, Brian Neal <bgn...@gmail.com> wrote:
> Oh...you are trying to do this in the *admin*? Are you sure you just
> don't need to "exclude" the form field?

Yep, that's what I needed. I already had an admin class for the
model, so it was just a matter of adding the line to exclude the
field.

For some reason I thought django would complain if there wasn't a
field (hidden or not) in the form for each of the model class
attributes. Not sure why I thought that. Excluding the field is the
way to go.

Thanks
Reply all
Reply to author
Forward
0 new messages