Example 6. testForm.html
$def with(form) <h1>Test Form</h1> <form method="POST"> $:form.render() </form>Example 7. test04.py
# -*- coding:utf-8 -*-
# Form
import web
import web.form
urls = (
'/', 'testForm'
)
app = web.application(urls, globals())
render = web.template.render('templates')
test_form = web.form.Form(
web.form.Textbox('text'),
web.form.Checkbox('chk', value='hoge'), # なぜか value を入れないと True/False が取れない
web.form.Radio('radio', ['a', 'b', 'c']),
web.form.Dropdown('drop', ['A', 'B', 'C']),
web.form.Button('Click'),
)
class testForm:
def GET(self):
f = test_form()
return render.testForm(f)
def POST(self):
f = test_form()
if not f.validates():
return render.testForm(f)
else:
s = "text: %s, %s\n" % (f.d.text, f['text'].value)
s += "chk: %s\n" % (f.d.chk)
s += "radio: %s\n" % (f.d.radio)
s += "drop: %s\n" % (f.d.drop)
return s
if __name__ == "__main__":
web.internalerror = web.debugerror
app.run()
実行
> python test03.pyForm に入力した結果は次のように表示される
text: aaa, aaa chk: True radio: c drop: B
0 件のコメント:
コメントを投稿