如何从Web2Py中的表单提交中获取值

时间:2016-06-13 23:28:41

标签: web2py

我没有从“request.vars”或“request.post_vars”中获取表单提交的值,就像我想的那样。

以下是表单的HTML:

{{extend 'layout.html'}}
{{=message}}
<form id="LoginForm" name="FormLogin" action="{{URL()}}" method="post">
<div class="container">
        <div class="row">
        <div class="col-md-offset-5 col-md-3 col-sm-6">
        <div class="form-login">
            <h4>{{=T("Welcome")}}</h4>
            <input type="text" id="UserName" class="form-control input-sm chat-input" placeholder="{{=T("username")}}" />
            </br>
            <input type="text" id="UserPassword" class="form-control input-sm chat-input" placeholder="{{=T("password")}}" />
            </br>
            <div class="wrapper">
                <span class="group-btn">     
                    <button type="submit" class="btn btn-primary btn-md">{{=T("login")}} <i class="fa fa-sign-in"></i></button>
                </span>
                <span class="group-btn" style="float:right;">
                    <a href="{{=URL("prov_login", "register")}}" class="btn btn-success btn-md">{{=T("Register")}}</a>
                </span>
            </div>
        </div>

    </div>
</div>
</div>
</form>

我的控制器看起来像这样:

def index():
if request.vars>0:
    out_message = "Registration request received "
else:
    out_message = "no post"

return dict(message=out_message)

我错过了什么?为什么我没有得到输入值?

1 个答案:

答案 0 :(得分:1)

查看代码:

{{extend 'layout.html'}}
{{=message}}
<form>
    <div class="container">
        <div class="row">
            <div class="col-md-offset-5 col-md-3 col-sm-6">
                <div class="form-login">
                    <h4>{{=T("Welcome")}}</h4>
                    <input type="text" id="UserName" name="username" class="form-control input-sm chat-input" placeholder="{{=T("username")}}" />
                    </br>
                    <input type="text" id="UserPassword" name="password" class="form-control input-sm chat-input" placeholder="{{=T("password")}}" />
                    </br>
                    <div class="wrapper">
                        <span class="group-btn">     
                        <button type="submit" class="btn btn-primary btn-md">{{=T("login")}} <i class="fa fa-sign-in"></i></button>
                        </span>
                        <span class="group-btn" style="float:right;">
                            <a href="{{=URL("prov_login", "register")}}" class="btn btn-success btn-md">{{=T("Register")}}</a>
                        </span>
                    </div>
                </div>
            </div>
        </div>
    </div>
</form>

控制器代码:

def index():
    if request.vars>0:
        #out_message = "Registration request received"
# you can write this then you see all vars return after form submission
        out_message = request.vars 
    else:
        out_message = "no post"
    return dict(message=out_message)