[英]How to use the POST method in web.py to update a table in a postgreSQL database?
Here's my web.py code so far. 到目前为止,这是我的web.py代码。 I want to use my web server to update a table. 我想使用我的Web服务器更新表。
import web
urls = ('/', 'Index')
render = web.template.render('templates/')
app = web.application(urls, globals())
web.config.debug = True
db = web.database(dbn='postgres', db='my_db', user='postgres', pw='******', host='localhost')
class Index:
def GET(self):
drivers = db.select("drivers")
return render.index(drivers)
def POST(self, name):
return "post"
if __name__ == '__main__':app.run()
Right now it takes what's in the table and prints it out in the browser. 现在,它获取表中的内容并在浏览器中将其打印出来。 But I want to implement some logic that adds numeric values to the table as a while loop runs using the post method. 但是我想实现一些逻辑,该逻辑在使用post方法运行while循环时将数字值添加到表中。 How can I do this? 我怎样才能做到这一点?
You can do the Postgres insertion as follows, 您可以按以下方式进行Postgres插入,
def POST(self):
sequence_id = db.insert('test1', col1=5, col2=3) # insert(tablename, seqname=None, _test=False, **values)
Refer this for more details. 有关更多详细信息,请参考此内容。
You may use, 您可以使用,
data = json.loads(web.data()) # convert to dictionary if needed using json.loads
or 要么
data = web.input().number # number is the key which holds the data
depending on how you send data to POST method. 取决于您如何将数据发送到POST方法。
eg: If content-type = application/json
use web.input().number
例如:如果content-type = application/json
使用web.input().number
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.