首页 > 代码库 > Form 表单提交

Form 表单提交

简单方法(1)
#!/usr/bin/env python
# -*- coding:utf-8 -*-

import tornado.ioloop
import tornado.web
import re
class MainForm(object):
    def __init__(self):
        self.host = "(.*)"
        self.ip = "^(25[0-5]|2[0-4]\d|[0-1]?\d?\d)(\.(25[0-5]|2[0-4]\d|[0-1]?\d?\d)){3}$"
        self.port = ‘(\d+)‘
        self.phone = ‘^1[3|4|5|8][0-9]\d{8}$‘
       

    def chec_inpu(self,handler):
        exits = True
        inp_dict = {}
        for key,value in self.__dict__.items():
            user_input = handler.get_argument(key)
            res = re.match(value,user_input)
            if not res:
                exits = False
            inp_dict[key] = user_input
        return exits,inp_dict

class MainHandler(tornado.web.RequestHandler):
    def get(self):

        self.render(‘index.html‘)
    def post(self, *args, **kwargs):
        obj = MainForm()
        ret,inp_dict = obj.chec_inpu(self)
        print(ret,inp_dict)
        self.render(‘index.html‘)

settings = {
    # 模版路劲配置
    ‘template_path‘:‘views‘,
    # 静态路劲配置 如css 和 js
    ‘static_path‘:‘static‘,
    #静态文件的前缀
    ‘static_url_prefix‘:‘/ssss/‘,

}
# 路由映射
application = tornado.web.Application([
    (r"/index", MainHandler),
] , **settings)


if __name__ == "__main__":
    application.listen(8888)
    tornado.ioloop.IOLoop.instance().start()

  

高级版()

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

import tornado.ioloop
import tornado.web
import re

class ReHandler(object):
    def __init__(self):
        self.host = "(.*)"
        self.ip = "^(25[0-5]|2[0-4]\d|[0-1]?\d?\d)(\.(25[0-5]|2[0-4]\d|[0-1]?\d?\d)){3}$"
        self.port = ‘(\d+)‘
        self.phone = ‘^1[3|4|5|8][0-9]\d{8}$‘

    def rehande(self,ret):

        re_dict = {
            ‘host‘: "(.*)",
            ‘ip‘:"^(25[0-5]|2[0-4]\d|[0-1]?\d?\d)(\.(25[0-5]|2[0-4]\d|[0-1]?\d?\d)){3}$",
            ‘port‘:"(\d+)",
            ‘phone‘:"^1[3|4|5|8][0-9]\d{8}$",
        }

        return  re_dict[ret]
# 匹配验证
class ReForm(ReHandler):

    def __init__(self,error_dict=None,required=True):
        self.error_dict = {}
        if error_dict:
            # 所以错误信息
            self.error_dict.update(error_dict)
        # 能否为空
        self.required = required
        # 错误指令
        self.error = None
        # True可以为空  默认不能为空
        self.is_valid = False
        # 用户value
        self.value = http://www.mamicode.com/None"/index", MainHandler),
] , **settings)


if __name__ == "__main__":
    application.listen(8888)
    tornado.ioloop.IOLoop.instance().start()

  

HTML

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
    <form method="post" action="/index">
        <input type="text" placeholder="host" name="host">{{ errors.get(‘host‘,"")}}
        <input type="text" placeholder="ip" name="ip">{{ errors.get(‘ip‘,"")}}
        <input type="text" placeholder="port" name="port">{{ errors.get(‘port‘,"")}}
        <input type="text" placeholder="phone" name="phone">{{ errors.get(‘phone‘,"")}}
        <input type="submit" placeholder="提交">
    </form>
</body>
</html>

  

Form 表单提交