I have a server-side webpy code like this:
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
in one of my HTML-templates (footer) there is a button “Logout” that runs a script on click:
jQuery('#logout').click(function(){
jQuery.post(path,{'logout':''}, function(){location.reload();});
});
Everything works fine in /home section, but when I try to logout from /clients/ pages, it raises an error: TypeError: POST() takes exactly 1 argument (2 given).
Question 1: Why it happens?
Question 2: Is there any way to make any methods run in POST on every page by default (not to copy self.logout() line in every class.
Question1:
The capture in the regex for clientsPage – (.*) tells web.py that you want to capture that part of the URL and pass it as an argument to the POST method. From your code, it looks like that is the client id.
Question2:
I’d just use a seperate URL for logout. You don’t need to have a logout for every page.