免费注册 查看新帖 |

Chinaunix

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

web.py 子程序使用post问题! [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2013-02-04 17:19 |只看该作者 |倒序浏览
本帖最后由 lonelysoul011 于 2013-02-05 17:23 编辑

主程序源码:
  1. #!/usr/bin/python
  2. # -*- coding: utf-8 -*-

  3. #print "Content-type: text/html\n"
  4. #print  "Heloo"

  5. import web
  6. import blog
  7. import pos
  8. import guzhang
  9. #import connmysql
  10. from web  import form

  11. urls = ('/','code',
  12.                 '/blog',blog.app_blog,
  13.                 '/pos',pos.app_pos,
  14.                 '/guzhang',guzhang.app_guzhang
  15.                 #'/pos/add',pos.app_pos
  16.                
  17.                 )

  18. #'/pos',pos.app_pos
  19. render=web.template.render('templates/')

  20. class code:
  21.         def GET(self):
  22.                 #return "Hello word!"
  23.                 return  render.index()
  24.                
  25.                 #+ path

  26.                
  27. if __name__=='__main__':
  28.         app = web.application(urls,globals())
  29.         app.run()
  30.        
复制代码
子程序源码
  1. #print  "Heloo"

  2. import web
  3. from web  import form

  4. urls = (
  5.         "","repos",
  6.         '/(.*)','guzhang',
  7.         '/add','add'
  8. )

  9. render=web.template.render('templates/')
  10. db=web.database(dbn='mysql',host='192.168.1.87',user='root',pw='123456',db='yunwei')

  11. class repos:
  12.         def GET(self):
  13.                 raise web.seeother("/")
  14.                
  15. class guzhang:
  16.         def GET(self,path):
  17.                 s_guzhangs = db.select('guzhang')
  18.                 return render.guzhang(s_guzhangs)
  19. #                return 'pos'
  20. class add:
  21.     def POST(self,path):
  22.                 i = web.input()
  23.                 #d = web.input()
  24.                 n = db.insert('guzhang', find_time=i.find_time,do_time=i.do_time,all_time=i.all_time,what_happen=i.what_happen,result=i.result,_test=True)
  25.                 raise web.seeother('/')
  26.                
  27. app_guzhang = web.application(urls,globals())
复制代码
templates 下guzhang.html 源码
  1. $def with (s_guzhangs)       
  2. <ul>
  3. <table border="1">
  4. <tr>
  5.         <th>序号</th>
  6.         <th>发现时间</th>
  7.         <th>处理时间</th>
  8.         <th>持续时间</th>
  9.         <th>故障现象</th>
  10.         <th>处理结果</th>
  11. </tr>
  12. $for s_guzhang in s_guzhangs:
  13.         <tr>
  14.                 <td>$s_guzhang.id</td>
  15.                 <td>$s_guzhang.find_time</td>
  16.                 <td>$s_guzhang.do_time</td>
  17.                 <td>$s_guzhang.all_time</td>
  18.                 <td>$s_guzhang.what_happen</td>
  19.                 <td>$s_guzhang.result</td>
  20.         </tr>
  21. </ul>

  22. <form method="post" action="add">
  23.         <p>发现时间 <input type="text" name="find_time" /> <input type="submit" value="Add" /> </p>
  24. </form>
复制代码
启动方法:
[root@mqA cgi-bin]# ./code.py
http://0.0.0.0:8080/

参考web.py官方手册,在code 下直接运行可以Post 插入数据,使用子程序无法post 插入数据 。
错误信息:
[04/Feb/2013 17:23:04] "HTTP/1.1 POST /guzhang/add" - 405 Method Not Allowed

貌似官方手册post介绍不多。本人才接触web.py和python。
谢谢各位!

论坛徽章:
0
2 [报告]
发表于 2013-02-04 20:12 |只看该作者
提示: 作者被禁止或删除 内容自动屏蔽

论坛徽章:
0
3 [报告]
发表于 2013-02-05 08:25 |只看该作者
回复 2# thinkc
谢谢你的回答,
按照你的解决方法,添加了参数,但是还是没办法提交!

   

论坛徽章:
0
4 [报告]
发表于 2013-02-05 10:05 |只看该作者
提示: 作者被禁止或删除 内容自动屏蔽

论坛徽章:
0
5 [报告]
发表于 2013-02-05 10:30 |只看该作者
提示: 作者被禁止或删除 内容自动屏蔽

论坛徽章:
0
6 [报告]
发表于 2013-02-05 10:46 |只看该作者
form的action应该是/add吧
  1. <form method="post" action="/add">
复制代码

论坛徽章:
0
7 [报告]
发表于 2013-02-05 15:40 |只看该作者
提示: 作者被禁止或删除 内容自动屏蔽

论坛徽章:
0
8 [报告]
发表于 2013-02-05 16:51 |只看该作者
回复 6# zsjbz

使用/add跳转后,跳转的连接 http://192.168.1.87:8080/add.
而我想要的连接是 http://192.168.1.87:8080/guzhang/add


   

论坛徽章:
0
9 [报告]
发表于 2013-02-05 16:56 |只看该作者
回复 4# thinkc

我使用的apache,直接运行web.py的程序,可以使用POST,只是在子程序里面使用 POST 有问题。
对于PUT的使用,暂时还没学会。非常感谢各位的回答。

下面是我直接运行的程序,使用的是官网的例子 :
http://webpy.org/docs/0.3/tutorial

主程序:
  1. #!/usr/bin/python
  2. # -*- coding: utf-8 -*-

  3. #print "Content-type: text/html\n"
  4. #print  "Heloo"

  5. import web
  6. from web  import form

  7. urls = ('/','code',
  8.         '/add','add'
  9. )
  10. render=web.template.render('templates/')
  11. db=web.database(dbn='mysql',host='192.168.1.87',user='root',pw='123456',db='test')

  12. class code:
  13.         def GET(self):
  14.                 #i=web.input(name=None)
  15.                 #return render.test(name)
  16.                 pepoles = db.select('pepole')
  17.                 return render.mysql(pepoles)
  18. class add:
  19.     def POST(self):
  20.         i = web.input()
  21.                 #m = web.input()
  22.                 #a = web.input()
  23.         n = db.insert('pepole', name=i.name,mobile=i.mobile,adrress=i.adrress)
  24.         raise web.seeother('/')

  25.                
  26. if __name__=='__main__':
  27.         app = web.application(urls,globals())
  28.         app.run()
  29.        
  30.        
  31.        
复制代码
template 的mysql.html 代码:
  1. $def with (pepoles)
  2. <ul>
  3. $for pepo in pepoles:
  4.     <p>$pepo.id  $pepo.name   $pepo.mobile $pepo.adrress </p>
  5. </ul>

  6. <form method="post" action="add">
  7. <p>姓名<input type="text" name="name" /> </p>
  8. <p>电话<input type="text" name="mobile" /> </p>
  9. <p>地址<input type="text" name="adrress" /> </p>
  10. <p><input type="submit" value="提交" /></p>
  11. </form>
复制代码
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

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

清除 Cookies - ChinaUnix - Archiver - WAP - TOP