2016-09-28 62 views
0

我在蟒蛇新和我做了一个节目从形式到数据库中添加数据我这里使用WAMP服务器是我app.py代码“预计缩进块错误”在Python

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)

任何一个帮助请问..?

回答

0

范围由python通过缩进确定。因此,您需要缩进try语句,因为它在函数POST中。此外,finally(和db.close())上的格式显示错误。请查看this SO question,此guidePep 8最佳实践代码格式指南。另外,请小心不要混淆空格和制表符,因为这可能会导致问题。

+0

坦克我不知道它... – Edison