"预期的缩进块错误"在python中

时间:2016-09-28 07:32:43

标签: python-2.7

我是python的新手,我做了一个程序,使用wamp服务器将数据从表单添加到数据库这里是我的app.py代码

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()

我的错误是第12行的预期阻止块(return.render.hello_form)

任何人都可以帮忙吗??

1 个答案:

答案 0 :(得分:0)

范围是通过缩进在python中确定的。因此,您需要缩进try语句,因为它在函数POST中。此外,finally(和db.close())上的格式看起来不对。查看this SO question,此guidePep 8最佳做法代码格式指南。另外,请注意不要混合空格和制表符,因为这会导致问题。