通过未填充条件创建的 Web2py CRUD.read() 记录
因此,我在 Web2py 中编写了一个函数,用于在条件下在数据库中的表中创建一条记录,但 Web2py 会在未满足条件的情况下创建记录,
这是
def buy_product():
price = price_set(db,auth,'product')
balance = limit(db,auth,'settings','account_balance')
if balance !=None:
if balance < price:
form=redirect(URL('order'))
else:
form=crud.create(db.letter)
if form.accepts(request.vars, session):
tax = float(postage(form.vars.tax_type).replace("-","."))
##########################
# I'm talking about this #
##########################
if balance < (price + tax):
response.flash='You don\'t have enough balance to buy this product'
redirect(URL('not_processed'))
else:
function_1(....)
...
...
update_field(db,auth,'settings','account_balance',-price)
response.flash='Done'
redirect(URL('products'))
pass
elif form.errors:
response.flash='Error 01'
else:
pass
###############################
else:
form=redirect(URL('settings'))
return dict(form=form)
当 Balance
Balance
Balance
价格+税
用户应该被重定向到not_processed
,而不在数据库中创建新记录。
但 web2py 将用户重定向到 not_processed 并创建记录,而不使用用户输入的信息执行此部分。所以用户看到他买了一些东西,但它还没有处理(见下文),
function_1(....)
...
...
update_field(db,auth,'settings','account_balance',-price)
response.flash='Done'
redirect(URL('products'))
pass
有什么想法吗?
谢谢
So i worte a function in Web2py to create a record into a table in the Database under a Condition, but Web2py create the record allthough that condition is not filled,
here is the function
def buy_product():
price = price_set(db,auth,'product')
balance = limit(db,auth,'settings','account_balance')
if balance !=None:
if balance < price:
form=redirect(URL('order'))
else:
form=crud.create(db.letter)
if form.accepts(request.vars, session):
tax = float(postage(form.vars.tax_type).replace("-","."))
##########################
# I'm talking about this #
##########################
if balance < (price + tax):
response.flash='You don\'t have enough balance to buy this product'
redirect(URL('not_processed'))
else:
function_1(....)
...
...
update_field(db,auth,'settings','account_balance',-price)
response.flash='Done'
redirect(URL('products'))
pass
elif form.errors:
response.flash='Error 01'
else:
pass
###############################
else:
form=redirect(URL('settings'))
return dict(form=form)
it's sepposed that when the Balance < price + tax
the user should be redirected to not_processed
without creating new record in the database.
but web2py redirect the user to not_processed
and create the record without executing this part with the entered information from the user. so the user see that he bought something, when it has not processed (see below)
function_1(....)
...
...
update_field(db,auth,'settings','account_balance',-price)
response.flash='Done'
redirect(URL('products'))
pass
any idea ??
thank you
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
Crud 在内部管理插入/更新,并且不使用 form.accepts。
您有两个选择:
1 - 使用 SQLFORM
2 - 使用 crud 事件
请注意,SQLFORM 与 crud 有点不同,SQLFORM 期望您在 form.accepts 方法中验证表单,而 crud 在内部执行此操作并使用事件作为 onvalidation、onaccept自定义验证。
Crud manages inserts/updates internally and it does not use the form.accepts.
You have two options:
1 - Use SQLFORM
2 - Use crud events
Note that SQLFORM is a bit different of crud, SQLFORM expects you to validate the form in form.accepts method, while crud does it internally and uses events as onvalidation, onaccept to custom validations.