form.py constructing dropdown menu

90 views
Skip to first unread message

anil

unread,
Aug 23, 2006, 4:03:13 AM8/23/06
to web.py
Old method i used:
-----------------------------
#for cat in $sorted($catz, key=lambda r: r.category)
<option value="$cat.id">
$cat.category
</option>
#end for

How do i specify id in drop down menu for form.py
global myform
myform = form.Form(
form.Dropdown('types',[(i.category, i.id) for i in catz]),

Hope it is not too rudimentary
THanks guys
Anil

hendry

unread,
Aug 23, 2006, 7:15:11 AM8/23/06
to web.py
Here is the form docs http://webpy.infogami.com/form in svn that has a
dropdown example:

http://svn.natalian.org/projects/flash/f.py
http://svn.natalian.org/projects/flash/templates/formtest.html

Not sure what you mean by 'id' here. You want to set the id element of
the html?

form.py is limited by the render function. See form.py.

Best wishes,

anil

unread,
Aug 23, 2006, 5:49:55 PM8/23/06
to web.py
dear hendry

#for cat in $sorted($catz, key=lambda r: r.category)
<option value="$cat.id">
$cat.category
</option>
#end for

using form.py
i m able to specify $cat.category but unable to specify value of the
option
which is what i store in db
and hence the question
thanks for both of your prompt replies...
Anil

Tommi

unread,
Aug 24, 2006, 8:37:02 AM8/24/06
to web.py
Hi Anil,

Do try writing your own Dropdown class that supports option values. It
will be a good exercise!

Also, let us know what you came up with. :)

blaf

unread,
Aug 24, 2006, 9:11:14 AM8/24/06
to web.py
I'm not sure I understand your question but don't forget to use the
fill function if you want to show your value selected (this is not
tested code):

class test:
self.form = form.Form(
form.Dropdown('category', ['cat1','cat2','etc...'],
description='Category')
)

def GET(self):
formvalues = web.query('select...') # your query where you have
a category value
form = self.form() # your form definition
form.fill(formvalues) # fill your form
web.render('test.html')

test.html
#filter Filter
$form.category.render()
#end filter

or in your current code specify selected value regarding to your query:

#for cat in $sorted($catz, key=lambda r: r.category)

<option value="$cat.id"#if $cat.id == $form.category.value#
selected="selected"#end if#>
$cat.category
</option>
#end for

Hope this helps.

anil

unread,
Aug 25, 2006, 5:38:17 PM8/25/06
to web.py
<option value="$cat.id">
i m unable to specify value = "$cat.id"
using current form.py
i m sorry i should have been clearer
thanks
anil

Tommi

unread,
Aug 26, 2006, 6:10:34 AM8/26/06
to web.py
Yes.

Dropdown class doesn't support this. Take a look at how it works, and
based on that you can very easily write a new one that will!

(I hope I'm not unpolite for trying to teach you how to fish.)

Jeremy

unread,
Aug 29, 2006, 2:25:33 PM8/29/06
to web.py
For what it's worth, here's my quick-and-dirty hack to Dropdown:

class Dropdown(Input):
def __init__(self, name, args, *validators, **attrs):
self.args = args
super(Dropdown, self).__init__(name, *validators, **attrs)

def render(self):
x = '<select name="%s"%s>\n'%(web.websafe(self.name),
self.addatts())
for arg in self.args:
desc = arg
if type(arg) in (tuple,list) and len(arg) == 2:
arg,desc = arg
if self.value == arg: select_p = ' selected="selected"'
else: select_p = ''
if arg == desc:
x += " <option"+select_p+">%s</option>\n" %
web.websafe(arg)
else:
x += " <option"+select_p+"
value=\"%s\">%s</option>\n"%(web.websafe(arg),web.websafe(desc))
x += '</select>\n'
return x

Population is then something along the lines of:

animals = [(0,"None")] + [(x.id,x.name) for x in web.select("animals")]
form.Form(form.Dropdown("animals",animals,description="Select an
animal"))

Of course, single strings are supported as well.

Jeremy

Tommi

unread,
Aug 30, 2006, 7:55:35 AM8/30/06
to web.py
Looks good.

You called it quick-and-dirty, so... Is there something that you know
you could write better?

:)

Reply all
Reply to author
Forward
0 new messages