Web2Py错误

时间:2013-01-15 08:07:36

标签: web2py

当我尝试运行Web2Py的代码时出现错误。

基本上我们必须编写一个查询数据库并显示可用航班的功能。

该功能定义如下:

def searchFlights():
    return dict()

def show():
    receivedFlights = request.vars.originCity+','+request.vars.destCity+','+request.vars.type+','+request.vars.deptDate+','+request.vars.arrivalDate+','+request.vars.vlassType+','+request.vars.noOfAdults+','+request.vars.noOfChildren+','+request.vars.noOfInfants
    return dict(txt1=recievedflights, flights=db().select(db.Flight.request.originCity+','+request.vars.destCity+','+request.vars.type+','+request.vars.deptDate+','+request.vars.arrivalDate+','+request.vars.classType+','+request.vars.noOfAdults+','+request.vars.noOfChildren+','+request.vars.noOfInfants)) 

我们现在得到的错误是:

  **File "K:/wdd-softw/web2py/applications/Assignment/controllers/default.py", line 106, in show
    receivedFlights = request.vars.originCity+','+request.vars.destCity+','+request.vars.type+','+request.vars.deptDate+','+request.vars.arrivalDate+','+request.vars.vlassType+','+request.vars.noOfAdults+','+request.vars.noOfChildren+','+request.vars.noOfInfants

TypeError:+不支持的操作数类型:'NoneType'和'str'**

2 个答案:

答案 0 :(得分:2)

第一个问题是,当首次加载表单的页面时,没有提交任何内容,因此request.vars为空,因此每个request.vars.some_var都返回None。相反,你可以这样做:

receivedFlights = ', '.join(request.vars.values()) if request.vars else 'Empty'

您的数据库选择会遇到同样的问题 - 您应该只在request.vars存在时运行查询。此外,您的查询不正确 - 您不能简单地将所有值连接成一个字符串并将其放在select()中。我建议您阅读DAL中有关正确查询语法的章节。它看起来应该更像:

db((db.Flight.originCity == request.vars.originCity) &
   (db.Flight.destCity == request.vars.destCity) &
   ...).select()

最后,您可以按照建议here

简化查询构造
query = reduce(lambda a, b: (a & b),
    (db.mytable[v] == request.vars[v] for v in request.vars if v != None))

答案 1 :(得分:0)

使用... + str(request.vars.noOfAdults)+ ...将数字字段转换为字符串。