为什么POST有两个参数?

时间:2012-01-12 10:48:40

标签: python post web.py

我有一个像这样的服务器端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模板(页脚)中有一个按钮“Logout”,它在点击时运行脚本:

jQuery('#logout').click(function(){
    jQuery.post(path,{'logout':''}, function(){location.reload();});
});

/ home部分一切正常,但是当我尝试从/ clients / pages注销时,会引发错误:TypeError:POST()只接受1个参数(给定2个)。

问题1:为什么会发生? 问题2:默认情况下,是否有任何方法可以在每个页面上的POST中运行任何方法(不要在每个类中复制self.logout()行。

1 个答案:

答案 0 :(得分:2)

问题1: clientPage的正则表达式中的捕获 - (。*)告诉web.py您要捕获URL的那一部分并将其作为参数传递给POST方法。从您的代码中,它看起来像是客户端ID。

问题2: 我只是使用一个单独的URL进行注销。您不需要为每个页面注销。