Webpy没有验证表单

时间:2014-08-01 12:55:45

标签: python forms web.py

只有在我第一次提出请求时,Webpy才会从/搜索页面验证表单。

因此,我从索引页面开始,将数据放入文本框,然后我看到该表单已正确验证,/搜索显示我想要的内容。在那之后,我尝试使用/ search页面中的表单,但它确实有效。

我使用Ubuntu,Python 2.7.3和webpy 0.37

#!/usr/bin/env python
# -*- coding: utf-8 -*-

import web

urls = (
    '/', 'Index',
    '/search/', 'Search',
)

render = web.template.render('templates/')
search_form = web.form.Form(
        web.form.Textbox('text',
            size=30,
            description='Text'),
        web.form.Button('Apply'),
        validators = [web.form.Validator("This field should be filled",
                                        lambda i: i.text != '')]
    )


class Index:

    def GET(self):
        form = search_form()
        text = 'Hello'
        return render.index(form, text)

    def POST(self):
        form = search_form()
        if not form.validates():
           text = 'Bad Text'
           return render.index(form, text)
        else:
           url = '?text=%s' % form.d.text
           web.seeother('/search/' + url)
class Search:

    def GET(self):
        form = search_form()
        input = web.input(text="None")
        text = input.text
        return render.search(form, text)

    def POST(self):
        form = search_form()
        if not form.validates():
           text = 'Bad Text'
           return render.index(form, text)
        else:
           url = '?text=%s' % form.d.text
           web.seeother('/search/' + url)

class MyApplication(web.application):
    def run(self, port=8080, *middleware):
        func = self.wsgifunc(*middleware)
        return web.httpserver.runsimple(func, ('127.0.0.1', port))

if __name__ == "__main__":
    app = MyApplication(urls, globals())
    app.run(port=8081)

模板index.html和search.html

$def with (form, text)
<form action="" method="POST">
$:form.render()
</form>

<h1>$text</h>

我试图多次解决这个问题,并用Google搜索关于表单处理的所有内容。 根据{{​​3}},我认为我的代码可以正常工作。也许看到其他的

有点不对劲

1 个答案:

答案 0 :(得分:0)

根据这个example,我试图将我的approch更改为处理来自url的输入。 因此,我没有使用标准的web.input()方法,而是将常规表达添加到网址

urls = (
    '/', 'Index',
    '/search/text=(.*)', 'Search',
)

并直接在类Search的主体中访问变量。

#variable text contains the regexp from url
class Search
    def GET(self, text):
        form = search_form()
        return render.search(form, str(text))  

最后,表单/搜索页面开始正常工作。现在,无论用户使用此表单多少次,它都会验证用户输入和页面/搜索打印权限输出。

工作代码:

import web

urls = (
    '/', 'Index',
    '/search/text=(.*)', 'Search',
)

render = web.template.render('templates/')

search_form = web.form.Form(
        web.form.Textbox(
            'text',
            web.form.regexp(r"\S+", "It mustn't be empty"),
            size=30,
            description='Text'),
        web.form.Button('Apply'),
    )

class Index:

    def GET(self):
        form = search_form()
        text = 'Hello'
        return render.index(form, text)

    def POST(self):
        form = search_form()
        if not form.validates():
           text = 'Bad Text'
           return render.index(form, text)
        else:
           url = '/search/text=%s' % form.d.text
           web.seeother(url)

class Search:

    def GET(self, text):
        form = search_form()
        return render.search(form, str(text))

    def POST(self, text):
        form = search_form()
        if not form.validates():
           text = 'Bad Text'
           return render.index(form, str(text))
        else:            
           url = '/search/text=%s' % form.d.text
           web.seeother(url)

class MyApplication(web.application):          
    def run(self, port=8080, *middleware):
        func = self.wsgifunc(*middleware)
        return web.httpserver.runsimple(func, ('127.0.0.1', port))

if __name__ == "__main__":
    app = MyApplication(urls, globals())
    app.run(port=8102)