Django:从HTML表单使用views.py获取字段值 [英] Django:get field values using views.py from html form

查看:132
本文介绍了Django:从HTML表单使用views.py获取字段值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,我的HTML表单如下:

Hello everyone I have an HTML form as follows:

<body class="">
    <div class="navbar navbar-static-top navbar-inverse"></div>
    <div style="height:20%; width:30%; margin-top:12%; margin-left:40%;">
        <div class="list-group">
            <form role="form">
            <br/>
                <div class="form-group input-group">
                    <span class="input-group-addon"><i class="fa fa-circle-o-notch"  ></i></span>
                    <input type="text" name="Username" class="form-control" placeholder="Username" />
                </div>
                <div class="form-group input-group">
                    <span class="input-group-addon"><i class="fa fa-tag"  ></i></span>
                    <input type="text" name="first_name" class="form-control" placeholder="FIRST_NAME" />
                </div>
                <div class="form-group input-group">
                    <span class="input-group-addon"><i class="fa fa-tag"  ></i></span>
                <input type="text" name="last_name" class="form-control" placeholder="LAST_NAME" />
                </div>
                ...
                ...
                <a href="/submit" class="btn btn-success ">POST</a>
            </form>
        </div>
    </div>
</body>

,点击帖子后,我将其重定向到views.py。谁能告诉我如何将表单中所有字段的字段值都放入views.py中。预先感谢

and after clicking on post i am redirecting it to views.py. can any one tell me how to get the field values of all the fields of the form into views.py. thanks in advance

推荐答案

每个输入都应具有名称属性:

Each input should have a name attribute:

<input type="text" name="username" class="form-control"
                                      placeholder="Desired Username" />

然后在您看来,您可以使用 request.POST <访问该数据/ code>或 request.GET 类似于字典的对象(这取决于方法属性的< form> 标签):

And then in your view you can access that data using request.POST or request.GET dictionary-like objects (it depends on the method attribute of the <form> tag):

def register(request):
    username = request.POST['username']
    ...

但是您不应该以这种PHPish方式呈现/处理表单。了解 django表单

But you shouldn't render/handle forms this PHPish way. Learn the django forms.

这篇关于Django:从HTML表单使用views.py获取字段值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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