HTML 表单将值传递给 web2py [英] HTML form passing values to web2py

查看:31
本文介绍了HTML 表单将值传递给 web2py的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在处理一个页面,用户可以在其中选择搜索表单 (html) 中的所有适当字段,然后将值传递给 web2py 查询.

I am currently working on a page where a user would select all the appropriate fields in a search form (html) which would than pass the values to the web2py query.

我在编写此查询时遇到问题?我想知道如何将 HTML 表单中的值实际传递给要针对数据库运行的 web2py 查询?

I am having an issue with writing this query? I am wondering how to actually pass the values from HTML form to the web2py query to be run against the database?

推荐答案

当您向 web2py 提交表单(通过 GET 或 POST)时,所有表单变量都将在 request.vars (有关更多信息,请参阅有关调度request 对象 以及关于 表单).

When you submit a form to web2py (via GET or POST), all the form variables will be available in request.vars (for more on this, see the book sections on Dispatching and the request object as well as the chapter on forms).

因此,您可以执行以下操作:

So, you could do something like:

def search():
    rows = None
    if request.vars:
        query = reduce(lambda a, b: (a & b),
            (db.mytable[var] == request.vars[var] for var in request.vars))
        rows = db(query).select()
    return dict(rows=rows)

注意,带有生成器表达式的 reduce() 相当于编写这样的查询:

Note, the reduce() with the generator expression is equivalent to writing a query like this:

(db.mytable.field1 == request.vars.field1) & \
(db.mytable.field2 == request.vars.field2) & \
...
(db.mytable.fieldN == request.vars.fieldN)

这篇关于HTML 表单将值传递给 web2py的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆