6. webpy - form

 

支援元素

  • Textbox - free form single line input (input type="text")
  • Password - free form single line that hides input (input type="password")
  • Textarea - free form multi line input (textarea)
  • Dropdown - mutually exclusive input for lists (select and options)
  • Radio - mutually exclusive input for a few options (input type="radio")
  • Checkbox - binary input (input type="checkbox")
  • Button - submit the form (button)

 


Form 的使用

 

template:

$def with(form)

<h1>Register</h1>
<form method="POST">
    $:form.render()
</form>

code.py

from web import form

vpass = form.regexp(r".{3,20}$", 'must be between 3 and 20 characters')
vemail = form.regexp(r".*@.*", "must be a valid email address")

register_form = form.Form(
    form.Textbox("username", description="Username"),
    form.Textbox("email", vemail, description="E-Mail"),
    form.Password("password", vpass, description="Password"),
    form.Password("password2", description="Repeat password"),
    form.Button("submit", type="submit", description="Register"),
    validators = [
        form.Validator("Passwords did't match", lambda i: i.password == i.password2)]
)

class register:
    def GET(self):
        f = register_form()
        return render.register(f)

    def POST(self):
        f = register_form()
        if not f.validates():
            return render.register(f)

 


mytemplate:

$def with(form)

<form method="POST">
    $:form.render()
</form>

code:

from web import form

def __init__(self):
    vpass = form.regexp(r".{6,8}$", 'must be between 6 and 8 characters')            
    vemail = form.regexp(r".*@.*", "must be a valid email address")                
    self.tpl = web.template.render('templates/',base='layout')
    self.update_form = form.Form(
          form.Password("password1", vpass, description="New Password"),
          form.Password("password2", description="Re-type Password"),
          form.Button("submit", type="submit", description="Register"),
          validators = [form.Validator("Passwords did't match", lambda i: i.password1 == i.password2)]
    )

        def GET(self):
                checklogin()
                myf = self.update_form()
                return self.tpl.enduser(myf)
        def POST(self):
                checklogin()
                myf = self.update_form()
                if not myf.validates():
                        return self.tpl.enduser(myf)
                else:
                        pass


 


textbox:

 

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
    size="12",
    maxlength="12"
)

 


Dropdown:

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

 

 


Radio:

form.Radio('MyGroup', ['G1', 'G2', 'G3'])

 


Test Output

 

>>> f=form.Form(
...     form.Radio('MyGroup', ['G1', 'G2', 'G3'])
... )

>>> f.MyGroup.render()

u'<span><input type="radio" id="MyGroup" value="G1" name="MyGroup"/> G1<input type="radio" id="MyGroup" value="G2" name="MyGroup"/> G2<input type="radio" id="MyGroup" value="G3" name="MyGroup"/> G3</span>'

 

Creative Commons license icon Creative Commons license icon