Python 如何在web.py中形成有效性反馈
我想用html编写表单,比如Python 如何在web.py中形成有效性反馈,python,forms,web.py,Python,Forms,Web.py,我想用html编写表单,比如 form input type..... input type..... 然后用py写下: f = forms.register_form() if not f.validates(): return render.register(f) 问题是,如果表单未通过验证,我如何将信息反馈给用户。 velocity中有类似springbind的东西吗?让我用我的web.py项目中的一个片段来回答: bookeditform
form
input type.....
input type.....
然后用py写下:
f = forms.register_form()
if not f.validates():
return render.register(f)
问题是,如果表单未通过验证,我如何将信息反馈给用户。
velocity中有类似springbind的东西吗?让我用我的web.py项目中的一个片段来回答:
bookeditform = form.Form(
form.Textbox('title', form.notnull),
form.Textbox('author', form.notnull),
form.Textbox('year',
form.Validator('Must be a number', lambda x: not x or int(x) > 0)),
)
# ...
class NewBookHandler:
def GET(self):
f = bookeditform()
return render.bookedit(f, "New book")
def POST(self):
f = bookeditform()
if f.validates():
newid = db.insert('book', title=f.d.title,
author=f.d.author, year=f.d.year)
raise web.seeother('/book/%s' % newid)
else:
return render.bookedit(f, "New book")
重述:
在处理程序中,在GET时,只需呈现空表单。
在POST上,加载表单,并检查其是否有效。
如果有,请执行所需操作,并将用户重定向到成功页面。永远不要在POST上呈现任何内容,因为用户可以刷新页面,重新执行相同的操作。
如果没有,则重新呈现包含输入数据的表单,并显示错误消息。
请修改问题的格式。不要使用HTML,而是通过缩进4个空格将代码标记为代码。它不会向我显示任何错误消息。怎么办?请help@krishna222,我将form.notnull作为一些表单字段的验证器,而$:form.render实际上在标记为notnull的字段上显示Required。