介紹 web.py的表單模塊允許生成html表單,獲取用戶輸入,并在處理或?qū)⑵涮砑拥綌?shù)據(jù)庫之前對其進(jìn)行驗(yàn)證。
表單模塊定義了2個(gè)主要類:Form類和Input類。表單使用一個(gè)或多個(gè)輸入和可選驗(yàn)證器進(jìn)行實(shí)例化。輸入使用名稱變量以及可選參數(shù)和驗(yàn)證器進(jìn)行實(shí)例化。Input類被子類化為以下html輸入(parens中的html類型):
login = form.Form(
form.Textbox('username'),
form.Password('password'),
form.Button('Login'),
)
這定義了一個(gè)基本形式。一旦定義,你應(yīng)該再次調(diào)用它來獲取一個(gè)復(fù)制的實(shí)例,然后你可以在其上調(diào)用render方法,如下所示:
f = login()
print f.render()
這將輸出以下HTML:
<table>
<tr><th><label for="username">username</label></th><td><input type="text" id="username" name="username"/><div class="post" style="display: none;"></div></td></tr>
<tr><th><label for="password">password</label></th><td><input type="password" id="password" name="password"/><div class="post" style="display: none;"></div></td></tr>
<tr><th><label for="Login"></label></th><td><button id="Login" name="Login">Login</button><div class="post" style="display: none;"></div></td></tr>
</table>
輸入功能 表單輸入支持幾個(gè)附加屬性。例如:
form.textbox("firstname",
form.notnull, #put validators first followed by optional attributes
class_="textEntry", #gives a class name to the text box -- note the underscore
pre="pre", #directly before the text box
post="post", #directly after the text box
description="please enter your name", #describes field, defaults to form name ("firstname")
value="bob", #default value
id="nameid", #specify the id
)
除了上述屬性之外,還可以以相同的方式輸入任何html屬性。例如:
myform2 = form.Form(
form.textbox('phonenumber',
size="12",
maxlength="12" )
)
下拉列表
下拉輸入允許下拉列表中每個(gè)項(xiàng)目的唯一描述和值。為此,使用如下所示的元組創(chuàng)建下拉列表:
form.Dropdown('mydrop', [('value1', 'description1'), ('value2', 'description2')])
表格特征 除了單獨(dú)的輸入驗(yàn)證器,form.py還支持整個(gè)表單驗(yàn)證,允許比較字段。驗(yàn)證器作為變量'validators'作為列表傳遞。例如:
signup = form.Form(
form.Textbox('username'),
form.Password('password'),
form.Password('password_again'),
validators = [form.Validator("Passwords didn't match.", lambda i: i.password == i.password_again)]
)
表單數(shù)據(jù)發(fā)布后,可以輕松地將其放入數(shù)據(jù)庫(如果數(shù)據(jù)庫方案的名稱與您的webpy表單一致)。例如:
def POST(self):
f = myform()
if f.validates():
web.insert('data_table', **f.d)
#don't do web.insert('data_table', **web.input()) because malicious data could be submitted too
else:
render.foo(f)
例
import web
from web import form
render = web.template.render('templates/')
urls = ('/', 'index')
app = web.application(urls, globals())
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)
if __name__=="__main__":
web.internalerror = web.debugerror
app.run()
并示例formtest.html(將其放在templates子目錄中):
$def with (form)
<form name="main" method="post">
$if not form.valid: <p class="error">Try again, AmeriCAN:</p>
$:form.render()
<input type="submit" /> </form>
更多建議: