0

am new in python and i made a program to add data from a form to database am using wamp server here is my app.py code

import web
import MySQLdb
urls = (
    '/', 'Index'
)

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

render = web.template.render('templates/', base = "layout")
class Index(object):
    def GET(self):
    return render.hello_form()


def POST(self): #Open database connection
try:
db = MySQLdb.connect("localhost", "root", "", "testdb")

# prepare a cursor object using cursor() method
cursor = db.cursor()

# Prepare SQL query to INSERT a record into the database.
sql = "INSERT INTO details(name, address,) VALUES ("[self.name.text, self.address.text])""
try:
   # Execute the SQL command
   cursor.execute(sql)
   # Commit your changes in the database
   db.commit()
except:
   # Rollback in case there is any error
   db.rollback()

# disconnect from server
 db.close()



   finally:


form = web.input(name = "Nobody", greet = "Hello")
greeting = "%s, %s" % (form.name, form.address)
return render.index(greeting = greeting)



 if __name__ == '__main__':
        main()

and my error is Expected an indended block at line 12(return.render.hello_form)

can any one help pls..?

Edison Augusthy
  • 1,535
  • 17
  • 32

1 Answers1

0

Scope is determined in python by indentation. As a result, you need to indent the try statement as it is inside the function POST. Also, the formatting on finally (and db.close()) looks wrong. Check out the this SO question, this guide and the Pep 8 guide on best practice code formatting. Also, be careful not to mix spaces and tabs as this can cause problems.

Community
  • 1
  • 1
Ed Smith
  • 12,716
  • 2
  • 43
  • 55