【问题标题】:How to use the POST method in web.py to update a table in a postgreSQL database?如何使用 web.py 中的 POST 方法更新 postgreSQL 数据库中的表?
【发布时间】:2016-08-11 20:26:26
【问题描述】:

到目前为止,这是我的 web.py 代码。我想使用我的网络服务器来更新表格。

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

现在它获取表格中的内容并在浏览器中打印出来。但是我想实现一些逻辑,在使用 post 方法运行 while 循环时将数值添加到表中。我怎样才能做到这一点?

【问题讨论】:

    标签: python postgresql rest web.py


    【解决方案1】:

    您可以按如下方式插入 Postgres,

    def POST(self):
        sequence_id = db.insert('test1', col1=5, col2=3)  # insert(tablename, seqname=None, _test=False, **values)
    

    更多详情请参考this

    你可以使用,

    data = json.loads(web.data()) # convert to dictionary if needed using json.loads
    

    data = web.input().number # number is the key which holds the data
    

    取决于您如何将数据发送到 POST 方法。

    例如:如果content-type = application/json 使用web.input().number

    也请参考this

    【讨论】:

      猜你喜欢
      • 2020-04-07
      • 2020-07-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-04-26
      • 2020-12-29
      • 1970-01-01
      • 2021-11-22
      相关资源
      最近更新 更多