V2EX = way to explore
V2EX 是一个关于分享和探索的地方
Sign Up Now
For Existing Member  Sign In
推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
larkifly
V2EX  ›  Python

webpy 如何获取 form 的错误信息

  •  
  •   larkifly · Oct 29, 2014 · 3220 views
    This topic created in 4198 days ago, the information mentioned may be changed or developed.
    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”,应该怎么做?
    3 replies    2015-04-20 16:37:22 +08:00
    hijoker
        1
    hijoker  
       Apr 20, 2015
    我也有这个需求啊,楼主搞定没?
    akin520
        2
    akin520  
       Apr 20, 2015
    i = web.input()
    bax = i.get("bax")
    larkifly
        3
    larkifly  
    OP
       Apr 20, 2015
    @hijoker 后来是改webpy的源码实现的,webpy源码中已经有地方存储了这些错误信息,只是没提供接口获取,你加个接口就可以了。具体的方法就是在webpy的form.py(119行)文件的class input里加了一个函数def getnote(self):
    return self.note
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   3796 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 35ms · UTC 04:39 · PVG 12:39 · LAX 21:39 · JFK 00:39
    ♥ Do have faith in what you're doing.