Web.py – some quick references

Writing POST request
We use POST method when we have to submit some data that will make some changes in webserver. Inorder to invoke a POST request,specify the method attribute of html form as POST. Upon submission of form POST method will be called.

The above html form contain a textbox to read name. Now let’s look how to read the name entered into the textbox from inside a POST method.

def POST(self):
        name =  web.input()
        return 'Entered name is '+name.user

Note that the ‘user’ is the name attribute of textbox.
Usually we pass our values through ‘web.websafe()’ function due to security reasons. ( web.websafe(name.user))

Redirect to a new page after POST

def POST(self):
        name =  web.input()
        f = open("file.txt","a")
        raise web.seeother('/names')

The above function describe two attributes. First one is, how to write the input value into a file, and the second is how to redirect to a new page from a POST method.
Inorder to write into a file just open file in append mode.This is to write all names in each submission. Then write into file using write attribute of file.
Inorder to redirect to a new page we use web.seeother() function. Pass the url of page as arguement inside the function call. Here I have given the url ‘/names’ which contain all the names entered through textbox.


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s