如何通过服务调用使用 sqlform 更新数据库

发布于 2024-12-14 04:54:08 字数 1022 浏览 0 评论 0原文

我想知道如何通过服务调用使用 sqlform 更新数据库。例如

db.py 包含

db.define_table('person',
Field('name'),
Field('email'))

控制器包含

@service.run
def display_form():
form = SQLFORM(db.person)
if form.accepts(request.vars):
    return "success"
else:
    return "failure"

http post 变量作为 data = {"name":"xyz", "email":"[电子邮件受保护]"}

我想知道我是否正确发送后变量。我总是收到失败消息。为什么数据库没有更新。实际上我想用 form = auth.register() 来做到这一点。我需要通过服务调用输入身份验证表值。但这还有很长的路要走。

更新

这就是我发帖的方式。

url = "http://example.com/myapp/mycontroller/api/person"
data = {"name":"peter","email":"[email protected]"}
datatosend = urllib.urlencode(data)
request = urllib2.Request(url,datatosend)
res = urllib2.urlopen(req)

但是api中的POST方法根本看不到这个人。

I wanted to know how to update the database using sqlform through a service call. for example

db.py contains

db.define_table('person',
Field('name'),
Field('email'))

controller contains

@service.run
def display_form():
form = SQLFORM(db.person)
if form.accepts(request.vars):
    return "success"
else:
    return "failure"

The http post variable is sent as data = {"name":"xyz", "email":"[email protected]"}

I would like to know whether I am sending the post variables correctly. I always get the failure message. WHY IS THE DB NOT GETTING UPDATED. Actually I would like to do this with form = auth.register(). I need to enter the auth table values through a service call. but this is long way to go.

Update

This is the way I am posting.

url = "http://example.com/myapp/mycontroller/api/person"
data = {"name":"peter","email":"[email protected]"}
datatosend = urllib.urlencode(data)
request = urllib2.Request(url,datatosend)
res = urllib2.urlopen(req)

but the POST method in api is not seeing the person at all.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

半葬歌 2024-12-21 04:54:09

当您通过SQLFORM 创建表单时,一个特殊的隐藏_formname 字段将添加到表单中。如果发布的数据不包含匹配的 _formname 字段,则 form.accepts() 方法将失败。您可以指定自己的表单名称来代替默认的表单名称,并将其添加到发布的数据中:

@service.run
def display_form():
    request.vars._formname = 'myform'
    form = SQLFORM(db.person)
    if form.accepts(request.vars, formname='myform'):
        return 'success'
    else:
        return 'failure'

但是,一种更简单的方法是完全避免表单并直接插入记录(仍然通过使用 < 来利用验证过程) code>validate_and_insert 方法):

@service.run
def display_form():
    return db.person.validate_and_insert(**request.vars)

如果成功则返回插入的记录 ID,否则返回验证错误。

@service.run 的替代方案是新的 RESTful Web 服务 功能:

@request.restful()
def api():
    def POST(tablename, **fields):
        if not tablename == 'person': raise HTTP(400)
        return db.person.validate_and_insert(**fields)
    return locals()

然后通过 POST 请求将记录插入到:

http://mydomain.com/myapp/mycontroller/api/person

When you create a form via SQLFORM, a special hidden _formname field is added to the form. If the posted data does not include a matching _formname field, the form.accepts() method will fail. In place of the default formname, you can specify your own, and add it to the posted data:

@service.run
def display_form():
    request.vars._formname = 'myform'
    form = SQLFORM(db.person)
    if form.accepts(request.vars, formname='myform'):
        return 'success'
    else:
        return 'failure'

However, a much simpler method is to avoid the form altogether and directly insert the record (still taking advantage of the validation process by using the validate_and_insert method):

@service.run
def display_form():
    return db.person.validate_and_insert(**request.vars)

This will return the inserted record ID if successful or validation errors otherwise.

An alternative to @service.run is the new RESTful web services functionality:

@request.restful()
def api():
    def POST(tablename, **fields):
        if not tablename == 'person': raise HTTP(400)
        return db.person.validate_and_insert(**fields)
    return locals()

Then insert a record via a POST request to:

http://mydomain.com/myapp/mycontroller/api/person

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文