检查URL查询的条件

时间:2016-04-14 00:53:23

标签: python web2py

如何创建if ... else条件以检查以下内容:

if the URL ends with this /default/add?a=1&b=1
    compute something
else the URL ending with this /default/add/1/1
    compute this

所以,我的功能看起来像这样。我能够计算出第一个条件,但我不认为这是正确的。

def add():
if URL(scheme=True,args=[add]):
    a1 = int(request.vars.a)
    b1 = int(request.vars.b)
    return a1+b1
else:
    a2 = int(request.args[0])
    b2 = int(request.args[1])
    return a2+b2

当我尝试对/ default / add / 1/1进行测试时,出现以下错误:

a1 = int(request.vars.a)
TypeError: int() argument must be a string or a number, not 'NoneType'

3 个答案:

答案 0 :(得分:2)

用于#nav { list-style: none; margin: 0; padding: 0; margin-bottom: 20px; } #nav li { position: relative; margin: 0; font-size: 15px; border-bottom: 1px solid #fff; padding: 0; } #nav li ul { opacity: 0; height: 0px; } #nav li a { font-style: normal; font-weight: 400; position: relative; display: block; padding: 16px 25px; color: #fff; white-space: nowrap; z-index: 2; text-decoration: none } #nav li a:hover { color: #c0392b; background-color: #ecf0f1; } #nav ul li { background-color: #e74c3c; color: #fff; display: block; list-style: disc; } #nav li:first-child { border-top: 1px solid #fff; } #nav ul { margin: 0; padding: 0; } #nav .fa { margin: 0px 17px 0px 0px; } .logo { width: 100%; padding: 21px; margin-bottom: 20px; box-sizing: border-box; } #logo{ color: #fff; font-size: 30px; font-style: normal; } .sidebar-icon { position: relative; float: right; text-align: center; line-height: 1; font-size: 25px; padding: 6px 8px; color: #fff; } .disp { opacity: 1!important; height:auto!important; transition: height 100ms ease-in-out; transition-delay: 300ms; }

/default/add?a=1&b=1

a1 = int(request.vars['a']) b1 = int(request.vars['b']) 使用:

/default/add/1/1

答案 1 :(得分:0)

request.vars是一个dict,所以语法应该是:

BaseFunction

编辑:user1434070打败了我 - 对不起该骗局

答案 2 :(得分:0)

URL(scheme=True,args=[add])生成一个网址 - 它不会检查当前请求的网址。相反,你想要:

    if 'a' in request.vars and 'b' in request.vars: