免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 3382 | 回复: 1
打印 上一主题 下一主题

求助全能的CU大侠们,web.py的自定义验证解决了,可是又出现sqlite数据库提交错误 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2011-01-21 22:26 |只看该作者 |倒序浏览
本帖最后由 hlsd665 于 2011-01-22 00:08 编辑

官网上给的代码如下:
  1.     vpass = web.form.regexp(r".{3,20}$", '密码长度3到20位字符')
  2.     register_form = web.form.Form(
  3.         web.form.Textbox("username", description="用户名"),
  4.         web.form.Password("password", vpass, description="密 码"),
  5.         web.form.Password("password2", description="密 码"),
  6.         web.form.Button("submit", type="submit",html="添加"),
  7.         validators = [
  8.             web.form.Validator("两次密码不一致", lambda i: i.password == i.password2)]
  9.         )
复制代码
我想把验证用的lambda表达式换成自己定义的方法,用于验证输入框中提交的数据在数据库中的重复
  1.     client_form = web.form.Form(
  2.         web.form.Textbox("code",web.form.notnull,description="代码"),
  3.         web.form.Textbox("name",description="名称"),
  4.         web.form.Button("submit",type="submit",html="添加"),
  5.         validators = [
  6.             web.form.Validator('代码或名称重复', m_account.validclient)]

  7.         )
复制代码
验证方法m_account.validclient带有参数,按照原有lambda表达式能看出来只能给一个参数,应该是表单对象
可是象上面的方法调用时无论验证方法返回什么值,都不会通过验证。

万能的CU大侠们给看看。难死我了。拜谢了。

论坛徽章:
0
2 [报告]
发表于 2011-01-21 23:55 |只看该作者
本帖最后由 hlsd665 于 2011-01-22 00:04 编辑

自己建个验证类继承原有验证类,解决了,可是又出现数据库提交错误。是查询和提交的时间间隔太短了,还是两次调用数据库时分别建立了两个连接造成的冲突????????
错误代码如下:
  1. 0.0 (1): SELECT * FROM c_client WHERE years='2011' ORDER BY code
  2. 0.0 (2): SELECT * FROM c_client WHERE code='105'
  3. 0.02 (3): INSERT INTO c_client (code, name, years) VALUES ('105', '3213213', '2011')
  4. 0.0 (4): SELECT last_insert_rowid();
  5. Traceback (most recent call last):
  6.   File "C:\Python26\lib\site-packages\web\application.py", line 242, in process
  7.     return self.handle()
  8.   File "C:\Python26\lib\site-packages\web\application.py", line 233, in handle
  9.     return self._delegate(fn, self.fvars, args)
  10.   File "C:\Python26\lib\site-packages\web\application.py", line 415, in _delegate
  11.     return handle_class(cls)
  12.   File "C:\Python26\lib\site-packages\web\application.py", line 390, in handle_class
  13.     return tocall(*args)
  14.   File "F:\workspace\dev\finance.py", line 56, in POST
  15.     m_account.newclient(client_data.code,client_data.name,year='2011')
  16.   File "F:\workspace\dev\model\m_account.py", line 12, in newclient
  17.     dbh.insert('c_client',code=code,name=name,years=year)
  18.   File "C:\Python26\lib\site-packages\web\db.py", line 744, in insert
  19.     self.ctx.commit()
  20.   File "C:\Python26\lib\site-packages\web\db.py", line 504, in commit
  21.     ctx.db.commit()
  22. OperationalError: cannot commit transaction - SQL statements in progress

  23. 127.0.0.1:4275 - - [22/Jan/2011 00:04:04] "HTTP/1.1 POST /finance/clientsetup" - 500 Internal Server Error
复制代码
  1. #coding:utf-8
  2. import web

  3. from model import m_account

  4. from web import form
  5. class revalid(form.Validator):
  6.     def __init__(self,msg):
  7.         self.msg = msg
  8.    
  9.     def valid(self, value):
  10.         return m_account.recode(value)
  11.    
  12. urls = (
  13.     '/', 'index',
  14.     '/system','system',
  15.     '/clientsetup','clientsetup',
  16. )
  17. render = web.template.render('templates/finance',base='../base')


  18. class clientsetup:
  19.     client_form = web.form.Form(
  20.         web.form.Textbox("code",web.form.notnull,revalid('no requ'),description="代码"),
  21.         web.form.Textbox("name",web.form.notnull,description="名称"),
  22.         web.form.Button("submit",type="submit",html="添加")
  23.         )
  24.    
  25.    
  26.     def GET(self):
  27.         clist=m_account.getclientlist('2011')
  28.         f=self.client_form()
  29.         return render.allsetup('客户',clist,'添加客户',f)
  30.     def POST(self):
  31.         f=self.client_form()
  32.         clist=m_account.getclientlist('2011')
  33.         if not f.validates():
  34.             return render.allsetup('客户',clist,'添加客户',f)
  35.         else:
  36.             client_data = web.input()
  37.             m_account.newclient(client_data.code,client_data.name,year='2011')
  38.             raise web.seeother('/clientsetup')
复制代码
m_account代码如下:
  1. #-*-coding:utf-8-*-

  2. import web

  3. import dbcon

  4. dbh=dbcon.dbs()

  5. def getclientlist(year):
  6.     return dbh.select('c_client',where='years=$year',order = 'code',vars=locals())
  7. def newclient(code,name,year):
  8.     dbh.insert('c_client',code=code,name=name,years=year)
  9. def recode(code):
  10.     return not bool(dbh.select('c_client',where='code=$code',vars=locals()))
复制代码
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP