Chinaunix
标题:
web.py 子程序使用post问题!
[打印本页]
作者:
lonelysoul011
时间:
2013-02-04 17:19
标题:
web.py 子程序使用post问题!
本帖最后由 lonelysoul011 于 2013-02-05 17:23 编辑
主程序源码:
#!/usr/bin/python
# -*- coding: utf-8 -*-
#print "Content-type: text/html\n"
#print "Heloo"
import web
import blog
import pos
import guzhang
#import connmysql
from web import form
urls = ('/','code',
'/blog',blog.app_blog,
'/pos',pos.app_pos,
'/guzhang',guzhang.app_guzhang
#'/pos/add',pos.app_pos
)
#'/pos',pos.app_pos
render=web.template.render('templates/')
class code:
def GET(self):
#return "Hello word!"
return render.index()
#+ path
if __name__=='__main__':
app = web.application(urls,globals())
app.run()
复制代码
子程序源码
#print "Heloo"
import web
from web import form
urls = (
"","repos",
'/(.*)','guzhang',
'/add','add'
)
render=web.template.render('templates/')
db=web.database(dbn='mysql',host='192.168.1.87',user='root',pw='123456',db='yunwei')
class repos:
def GET(self):
raise web.seeother("/")
class guzhang:
def GET(self,path):
s_guzhangs = db.select('guzhang')
return render.guzhang(s_guzhangs)
# return 'pos'
class add:
def POST(self,path):
i = web.input()
#d = web.input()
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)
raise web.seeother('/')
app_guzhang = web.application(urls,globals())
复制代码
templates 下guzhang.html 源码
$def with (s_guzhangs)
<ul>
<table border="1">
<tr>
<th>序号</th>
<th>发现时间</th>
<th>处理时间</th>
<th>持续时间</th>
<th>故障现象</th>
<th>处理结果</th>
</tr>
$for s_guzhang in s_guzhangs:
<tr>
<td>$s_guzhang.id</td>
<td>$s_guzhang.find_time</td>
<td>$s_guzhang.do_time</td>
<td>$s_guzhang.all_time</td>
<td>$s_guzhang.what_happen</td>
<td>$s_guzhang.result</td>
</tr>
</ul>
<form method="post" action="add">
<p>发现时间 <input type="text" name="find_time" /> <input type="submit" value="Add" /> </p>
</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。
谢谢各位!
作者:
thinkc
时间:
2013-02-04 20:12
提示:
作者被禁止或删除 内容自动屏蔽
作者:
lonelysoul011
时间:
2013-02-05 08:25
回复
2#
thinkc
谢谢你的回答,
按照你的解决方法,添加了参数,但是还是没办法提交!
作者:
thinkc
时间:
2013-02-05 10:05
提示:
作者被禁止或删除 内容自动屏蔽
作者:
thinkc
时间:
2013-02-05 10:30
提示:
作者被禁止或删除 内容自动屏蔽
作者:
zsjbz
时间:
2013-02-05 10:46
form的action应该是/add吧
<form method="post" action="/add">
复制代码
作者:
thinkc
时间:
2013-02-05 15:40
提示:
作者被禁止或删除 内容自动屏蔽
作者:
lonelysoul011
时间:
2013-02-05 16:51
回复
6#
zsjbz
使用/add跳转后,跳转的连接
http://192.168.1.87:8080/add.
而我想要的连接是
http://192.168.1.87:8080/guzhang/add
作者:
lonelysoul011
时间:
2013-02-05 16:56
回复
4#
thinkc
我使用的apache,直接运行web.py的程序,可以使用POST,只是在子程序里面使用 POST 有问题。
对于PUT的使用,暂时还没学会。非常感谢各位的回答。
下面是我直接运行的程序,使用的是官网的例子 :
http://webpy.org/docs/0.3/tutorial
主程序:
#!/usr/bin/python
# -*- coding: utf-8 -*-
#print "Content-type: text/html\n"
#print "Heloo"
import web
from web import form
urls = ('/','code',
'/add','add'
)
render=web.template.render('templates/')
db=web.database(dbn='mysql',host='192.168.1.87',user='root',pw='123456',db='test')
class code:
def GET(self):
#i=web.input(name=None)
#return render.test(name)
pepoles = db.select('pepole')
return render.mysql(pepoles)
class add:
def POST(self):
i = web.input()
#m = web.input()
#a = web.input()
n = db.insert('pepole', name=i.name,mobile=i.mobile,adrress=i.adrress)
raise web.seeother('/')
if __name__=='__main__':
app = web.application(urls,globals())
app.run()
复制代码
template 的mysql.html 代码:
$def with (pepoles)
<ul>
$for pepo in pepoles:
<p>$pepo.id $pepo.name $pepo.mobile $pepo.adrress </p>
</ul>
<form method="post" action="add">
<p>姓名<input type="text" name="name" /> </p>
<p>电话<input type="text" name="mobile" /> </p>
<p>地址<input type="text" name="adrress" /> </p>
<p><input type="submit" value="提交" /></p>
</form>
复制代码
欢迎光临 Chinaunix (http://bbs.chinaunix.net/)
Powered by Discuz! X3.2