所有web.py表单示例都采用以下格式(来自webpy.org):
myform = form.Form( form.TextBox("boe"),form.TextBox("bax",form.notnull,form.regexp('\d+','Must be a digit'),form.Validator('Must be more than 5',lambda x:int(x)>5)),form.Textarea('moe'),form.CheckBox('curly'),form.Dropdown('french',['mustard','fries','wine'])) class index: def GET(self): form = myform() # make sure you create a copy of the form by calling it (line above) # Otherwise changes will appear globally return render.formtest(form) def POST(self): form = myform() if not form.validates(): return render.formtest(form) else: # form.d.boe and form['boe'].value are equivalent ways of # extracting the validated arguments from the form. return "Grrreat success! boe: %s,bax: %s" % (form.d.boe,form['bax'].value)
我不希望在声明表单时填充下拉框(上例中的form.Dropdown)静态,但是在GET / POST方法中使用在调用页面时从数据库表中检索的条目.
解决方法
我建议你创建其他元素和表单,然后在GET / POST中创建你想要的下拉元素,并:
# Create copy of the form form = myform() # Append the dropdown to the form elements. form.inputs = tuple(list(form.inputs) + [mydropdown])