Monday 18 July 2011

A simple exercise in web.py

 This is just a simple exercise that will help you to understand the basics of web.py.

Qn: Create a webpage that should contain a text box,so that you can input your names,the page should also provide a submit button to submit the details that you entered in a text box.when you submit it the names should be saved in a text file and whole the names that we entered should be displayed in another URL.

1st step: Import the module Web.py,to use the tools provided by web.py. Map the first URL (" / ") to a class named "main".and map the second URL ("/Details") to a class named  "details".


import web

  urls = (
    '/','main',
    '/Details','details')

  app = web.application(urls, globals(), True)


2nd step: Next we have to define the class named "main".The function GET() is actually displays the content in your browser when you opens a webpage.When you opens the first page it should displays a form,so thats why ,we are writing the HTML code to create a form under GET() function.Here we are specifying the method attribute of form as POST to invoke the POST request .

class main:
    def GET(self):
    return'''
    <html>
    <head>
    <title>web.py</title>
    </head>

    <body>

    <h1>A simple exercise in web.py</h1>

    <form name="form" method = "POST">

    Name: <input type="text" name="user"/>

    <input type="submit" value="Submit" />

    </form>
    </body>
    </html>'''



3rd step: The GET() function that we introduced above will only serve the pages ,but here we have to update the values to another webpage.Thats why we are using a POST function.The line "form = web.input()" creates a storage objects that contains all  variables passed into it.Then we open a file named "file.txt" in append mode,then write the input values  into the file by using " f.write(form.user) ". Then Web.redirect('/Details') will redirect the user to a new URL(" /Details ") ,i.e when we submit the form it will guide you to the next page that contain all the details that you entered before.

def POST(self):

   form =  web.input()
   f=open("file.txt",'a')
   f.write(form.user)
   f.write("\n")
   f.close()

   raise web.redirect('/Details')

4th step: We have to define the class for the URL("/Define").Here we are using GET() function to display the contents.so to displays the contents in the text file we just simply opens the file in read mode.and then read the values by using "f.read()" function and then assigned it to a variable named data,when we return the variable it will displays all the values that we read from the text file in the next URL.

class details:

  def GET(self):

    f=open("file.txt",'r')
    data=f.read()
    return data
    f.close()


Complete code:


Program name: Code.py

import web

urls = (
  '/','main',
  '/Details','details')

app = web.application(urls, globals(), True)

class main:
   def GET(self):
    return'''
    <html>
    <head>
    <title>web.py</title>
    </head>

    <body>

    <h1>A simple exercise in web.py</h1>

    <form name="form" method = "POST">

    Name: <input type="text" name="user"/>

    <input type="submit" value="Submit" />

    </form>
    </body>
    </html>'''

    def POST(self):
 
      form =  web.input()
      f=open("file.txt",'a')
      f.write(form.user)
      f.write("\n")
      f.close()

    
      raise web.redirect('/Details')
    
class details:
    
    def GET(self):

      f=open("file.txt",'r')
      data=f.read()
      return data
      f.close()

if __name__ == "__main__":
    app.run()

No comments:

Post a Comment

Note: only a member of this blog may post a comment.