通过未填充条件创建的 Web2py CRUD.read() 记录

发布于 2024-12-13 16:44:36 字数 1824 浏览 0 评论 0原文

因此,我在 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 技术交流群。

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

发布评论

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

评论(1

三人与歌 2024-12-20 16:44:36

Crud 在内部管理插入/更新,并且不使用 form.accepts。

您有两个选择:

1 - 使用 SQLFORM

    form=SQLFORM(db.letter)
    if form.accepts(request.vars, session):
        tax = float(postage(form.vars.tax_type).replace("-","."))
        ##########################
        # I'm talking about this #
        ##########################

2 - 使用 crud 事件

def myfunction(form):
    # do your stuff here
    # it will be called only when form is accepted

def myotherfunction(form):
    if form.errors:
        #do something here in case of errors
        #it will be called during the form validation

crud.settings.create_onvalidation = myotherfunction
crud.settings.create_onaccept = myfunction
#the above can be:
#crud.create_onaccept = lambda form: myfunction(form)

# define the above events before the creation of the form
form=crud.create(db.letter)

请注意,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

    form=SQLFORM(db.letter)
    if form.accepts(request.vars, session):
        tax = float(postage(form.vars.tax_type).replace("-","."))
        ##########################
        # I'm talking about this #
        ##########################

2 - Use crud events

def myfunction(form):
    # do your stuff here
    # it will be called only when form is accepted

def myotherfunction(form):
    if form.errors:
        #do something here in case of errors
        #it will be called during the form validation

crud.settings.create_onvalidation = myotherfunction
crud.settings.create_onaccept = myfunction
#the above can be:
#crud.create_onaccept = lambda form: myfunction(form)

# define the above events before the creation of the form
form=crud.create(db.letter)

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.

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