Django:使用html表单中的views.py获取字段值

时间:2015-03-24 09:08:46

标签: django django-forms django-templates django-views html-form-post

大家好,我有一个html表格如下

<!DOCTYPE html>
<html>
    <head>



                <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
                <meta name="robots" content="NONE,NOARCHIVE" />


            <title>PROFILE</title>



                    <link rel="stylesheet" type="text/css" href="/static/rest_framework/css/bootstrap.min.css"/>
                    <link rel="stylesheet" type="text/css" href="/static/rest_framework/css/bootstrap-tweaks.css"/>

                <link rel="stylesheet" type="text/css" href="/static/rest_framework/css/prettify.css"/>
                <link rel="stylesheet" type="text/css" href="/static/rest_framework/css/default.css"/>



    </head>


    <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>


                                         <div class="form-group input-group">

                                            <span class="input-group-addon"></span>

                                            <input type="text" name="email" class="form-control" placeholder="Email" />

                                        </div>

                                      <div class="form-group input-group">

                                            <span class="input-group-addon"><i class="fa fa-lock"  ></i></span>

                                            <input type="text" name="contact_number" class="form-control" placeholder="CONTACT_NUMBER" />

                                        </div>

                    <div class="form-group input-group">

                                            <span class="input-group-addon"><i class="fa fa-lock"  ></i></span>

                                            <input type="text" name="mdn_number" class="form-control" placeholder="MDN_NUMBER" />

                                        </div>



                    <div class="form-group input-group">

                                            <span class="input-group-addon"><i class="fa fa-lock"  ></i></span>

                                            <input type="text" name="mobile_number" class="form-control" placeholder="MOBILE_NUMBER" />

                                        </div>


                    <div class="form-group input-group">

                                            <span class="input-group-addon"><i class="fa fa-lock"  ></i></span>

                                            <input type="text" name="address" class="form-control" placeholder="ADDRESS" />

                                        </div>

                                        <div class="form-group input-group">

                                            <span class="input-group-addon"><i class="fa fa-lock"  ></i></span>

                                            <input type="text" name="country" class="form-control" placeholder="COUNTRY" />

                                        </div>


                                        <div class="form-group input-group">

                                            <span class="input-group-addon"><i class="fa fa-lock"  ></i></span>

                                            <input type="text" name="state" class="form-control" placeholder="STATE" />

                                        </div>




                                     <div class="form-group input-group">

                                            <span class="input-group-addon"><i class="fa fa-lock"  ></i></span>

                                            <input type="text" name="pincode" class="form-control" placeholder="PINCODE" />

                                        </div>



                                     <a href="/submit" class="btn btn-success ">POST</a>



                                    </form>









  </div>
</div>

</body>
</html>

点击帖后我将其重定向到views.py。任何人都可以告诉我如何将表单的所有字段的字段值放入views.py。提前谢谢

2 个答案:

答案 0 :(得分:8)

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

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

然后在您的视图中,您可以使用request.POSTrequest.GET类字典对象访问该数据(具体取决于method标记的<form>属性):

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

但你不应该以这种PHP方式呈现/处理表单。了解django forms

答案 1 :(得分:0)

捕获python后端中的数据

POST请求

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

获取GET请求

def register(request):
    username = request.GET.get['username']
早期回答中

.get缺失