webpy 如何获取 form 的错误信息

2014-10-29 15:25:29 +08:00
 larkifly
webpy自带的form,当验证出错了,会生成包含有错误信息的html,我现在的需求是,只想获取到单纯的错误信息,不包含任何html标签。举个例子:

<form name="main" method="post">
<p class="error"> Try again, AmeriCAN:</p>
<table>
<tr>
<th>
<label for="boe">boe</label>
</th>
<td>
<input type="text" id="boe" value="12" name="boe"/>
</td>
</tr>
<tr>
<th>
<label for="bax">bax</label>
</th>
<td>
<input type="text" id="bax" value="1" name="bax"/>
<strong class="wrong">Must be more than 5</strong>
</td>
</tr>
<tr>
<th>
<label for="moe">moe</label>
</th>
<td>
<textarea id="moe" name="moe">aaaaa</textarea>
</td>
</tr>
<tr>
<th>
<label for="curly_">curly</label>
</th>
<td>
<input checked="checked" type="checkbox" id="curly_" value="" name="curly"/>
</td>
</tr>
<tr>
<th>
<label for="french">french</label>
</th>
<td>
<select id="french" name="french">
<option value="mustard">mustard</option>
<option value="fries">fries</option>
<option value="wine">wine</option>
</select>
</td>
</tr>
</table>
<input type="submit" />
</form>
上面是验证出错后返回的form,我现在只想要“Must be more than 5”,应该怎么做?
2682 次点击
所在节点    Python
3 条回复
hijoker
2015-04-20 11:32:17 +08:00
我也有这个需求啊,楼主搞定没?
akin520
2015-04-20 12:05:03 +08:00
i = web.input()
bax = i.get("bax")
larkifly
2015-04-20 16:37:22 +08:00
@hijoker 后来是改webpy的源码实现的,webpy源码中已经有地方存储了这些错误信息,只是没提供接口获取,你加个接口就可以了。具体的方法就是在webpy的form.py(119行)文件的class input里加了一个函数def getnote(self):
return self.note

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/142391

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX