为什么POST有两个参数?
我有一段服务器端的webpy代码,像这样:
urls = (
'/home', 'homePage',
'/clients/(.*)', 'clientsPage',
)
# Class for common pages methods and parameters
class allpages(object): ....
def logout(self):
i=web.input().keys()[0]
if i=='logout': session.kill()
return
class homePage(allpages):
def GET (self):
self.loginCheck()
return self.showpage('home',self.userName())
def POST (self):
self.logout()
return
class clientsPage(allpages):
def GET (self, client):
self.loginCheck()
if client == '': clientID=renderInc.firmlist('clients')
elif client == 'new': clientID=renderInc.newfirm('clients')
else: clientID = 'There is no such client' #TODO: make a 404 page
return self.showpage('clients',clientID)
def POST (self):
self.logout()
return
在我的一个HTML模板(底部)里,有一个“注销”按钮,点击后会运行一个脚本:
jQuery('#logout').click(function(){
jQuery.post(path,{'logout':''}, function(){location.reload();});
});
在/home这个部分一切都正常,但当我尝试在/clients/页面注销时,就出现了一个错误:TypeError: POST() 需要1个参数(给了2个)。
问题1:为什么会这样?
问题2:有没有办法让每个页面默认都能运行POST方法,而不需要在每个类里都复制self.logout()
这一行?
1 个回答
2
问题1: 在clientsPage的正则表达式中,(.*)的部分告诉web.py你想要抓取这个URL的一部分,并把它当作参数传递给POST方法。从你的代码来看,这部分应该是客户端的ID。
问题2: 我建议你为登出使用一个单独的URL。你不需要为每个页面都设置一个登出功能。