如何通过服务调用使用 sqlform 更新数据库
我想知道如何通过服务调用使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
当您通过
SQLFORM
创建表单时,一个特殊的隐藏_formname
字段将添加到表单中。如果发布的数据不包含匹配的_formname
字段,则form.accepts()
方法将失败。您可以指定自己的表单名称来代替默认的表单名称,并将其添加到发布的数据中:但是,一种更简单的方法是完全避免表单并直接插入记录(仍然通过使用 < 来利用验证过程) code>validate_and_insert 方法):
如果成功则返回插入的记录 ID,否则返回验证错误。
@service.run
的替代方案是新的 RESTful Web 服务 功能:然后通过 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, theform.accepts()
method will fail. In place of the default formname, you can specify your own, and add it to the posted data: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):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:Then insert a record via a POST request to:
http://mydomain.com/myapp/mycontroller/api/person