web.py 表單

2022-04-24 18:21 更新

介紹 web.py的表單模塊允許生成html表單,獲取用戶輸入,并在處理或?qū)⑵涮砑拥綌?shù)據(jù)庫之前對其進行驗證。

表單模塊定義了2個主要類:Form類和Input類。表單使用一個或多個輸入和可選驗證器進行實例化。輸入使用名稱變量以及可選參數(shù)和驗證器進行實例化。Input類被子類化為以下html輸入(parens中的html類型):

  • 文本框 - 自由格式單行輸入(輸入類型=“文本”)
  • 密碼 - 隱藏輸入的單行代碼(輸入類型=“密碼”)
  • Textarea - 自由形式多線輸入(textarea)
  • 下拉列表 - 列表的互斥輸入(選擇和選項)
  • 無線電 - 幾個選項的互斥輸入(輸入類型=“無線電”)
  • 復選框 - 二進制輸入(輸入類型=“復選框”)
  • 按鈕 - 提交表單(按鈕) 基本登錄表單如下所示:

  1. login = form.Form(
  2. form.Textbox('username'),
  3. form.Password('password'),
  4. form.Button('Login'),
  5. )

這定義了一個基本形式。一旦定義,你應(yīng)該再次調(diào)用它來獲取一個復制的實例,然后你可以在其上調(diào)用render方法,如下所示:

  1. f = login()
  2. print f.render()

這將輸出以下HTML:

  1. <table>
  2. <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>
  3. <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>
  4. <tr><th><label for="Login"></label></th><td><button id="Login" name="Login">Login</button><div class="post" style="display: none;"></div></td></tr>
  5. </table>

輸入功能 表單輸入支持幾個附加屬性。例如:

  1. form.textbox("firstname",
  2. form.notnull, #put validators first followed by optional attributes
  3. class_="textEntry", #gives a class name to the text box -- note the underscore
  4. pre="pre", #directly before the text box
  5. post="post", #directly after the text box
  6. description="please enter your name", #describes field, defaults to form name ("firstname")
  7. value="bob", #default value
  8. id="nameid", #specify the id
  9. )

除了上述屬性之外,還可以以相同的方式輸入任何html屬性。例如:

  1. myform2 = form.Form(
  2. form.textbox('phonenumber',
  3. size="12",
  4. maxlength="12" )
  5. )

下拉列表

下拉輸入允許下拉列表中每個項目的唯一描述和值。為此,使用如下所示的元組創(chuàng)建下拉列表:

form.Dropdown('mydrop', [('value1', 'description1'), ('value2', 'description2')])

表格特征 除了單獨的輸入驗證器,form.py還支持整個表單驗證,允許比較字段。驗證器作為變量'validators'作為列表傳遞。例如:

  1. signup = form.Form(
  2. form.Textbox('username'),
  3. form.Password('password'),
  4. form.Password('password_again'),
  5. validators = [form.Validator("Passwords didn't match.", lambda i: i.password == i.password_again)]
  6. )

表單數(shù)據(jù)發(fā)布后,可以輕松地將其放入數(shù)據(jù)庫(如果數(shù)據(jù)庫方案的名稱與您的webpy表單一致)。例如:

  1. def POST(self):
  2. f = myform()
  3. if f.validates():
  4. web.insert('data_table', **f.d)
  5. #don't do web.insert('data_table', **web.input()) because malicious data could be submitted too
  6. else:
  7. render.foo(f)

  1. import web
  2. from web import form
  3. render = web.template.render('templates/')
  4. urls = ('/', 'index')
  5. app = web.application(urls, globals())
  6. myform = form.Form(
  7. form.Textbox("boe"),
  8. form.Textbox("bax",
  9. form.notnull,
  10. form.regexp('\d+', 'Must be a digit'),
  11. form.Validator('Must be more than 5', lambda x:int(x)>5)),
  12. form.Textarea('moe'),
  13. form.Checkbox('curly'),
  14. form.Dropdown('french', ['mustard', 'fries', 'wine']))
  15. class index:
  16. def GET(self):
  17. form = myform()
  18. # make sure you create a copy of the form by calling it (line above)
  19. # Otherwise changes will appear globally
  20. return render.formtest(form)
  21. def POST(self):
  22. form = myform()
  23. if not form.validates():
  24. return render.formtest(form)
  25. else:
  26. # form.d.boe and form['boe'].value are equivalent ways of
  27. # extracting the validated arguments from the form.
  28. return "Grrreat success! boe: %s, bax: %s" % (form.d.boe, form['bax'].value)
  29. if __name__=="__main__":
  30. web.internalerror = web.debugerror
  31. app.run()

并示例formtest.html(將其放在templates子目錄中):

  1. $def with (form)
  2. <form name="main" method="post">
  3. $if not form.valid: <p class="error">Try again, AmeriCAN:</p>
  4. $:form.render()
  5. <input type="submit" /> </form>
以上內(nèi)容是否對您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號
微信公眾號

編程獅公眾號