一、 文本域和单选按钮
前端 文本域和单选按钮表单获取方法一样
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
<
form
class
=
"form-horizontal"
method
=
"post"
>
<
div
class
=
"form-group"
>
<
label
for
=
"username"
class
=
"col-sm-2 control-label"
>Username</
label
>
<
div
class
=
"col-sm-10"
>
<
input
type
=
"text"
class
=
"form-control"
id
=
"username"
name
=
"username"
placeholder
=
"Username"
autofocus
=
""
>
</
div
>
</
div
>
....
...
<
div
class
=
"row"
>
<
div
class
=
"col-sm-2 col-sm-offset-2"
>
<
input
name
=
"sex"
value
=
"M"
type
=
"radio"
checked
=
"checked"
> 男
</
div
>
<
div
class
=
"col-sm-2 "
>
<
input
name
=
"sex"
value
=
"F"
type
=
"radio"
> 女
</
div
>
</
div
>
</
form
>
|
文本
pyhon 后端用request.values.get("name") 去获取
1
2
3
|
if
request.method
=
=
"POST"
:
username
=
request.values.get(
"username"
)
sex
=
request.values.get(
"sex"
)
|
二、多选按钮checkbox
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
<
div
class
=
"row"
>
<
div
class
=
"col-sm-2 "
>
<
label
>
<
input
name
=
"s_option"
value
=
"basketball"
type
=
"checkbox"
> 篮球
</
label
>
</
div
>
<
div
class
=
"col-sm-2 "
>
<
label
>
<
input
name
=
"s_option"
value
=
"football"
type
=
"checkbox"
> 足球
</
label
>
</
div
>
<
div
class
=
"col-sm-2 "
>
<
label
>
<
input
name
=
"s_option"
value
=
"badminton"
type
=
"checkbox"
> 羽毛球
</
label
>
</
div
>
</
div
>
|
后端使用request.values.getlist("s_option") 去获取,获取的value会组成一个列表
1
2
3
|
s_option
=
request.values.getlist(
"s_option"
)
for
s
in
s_option:
pass
|
三、select 下拉框
前端表现形式
1
2
3
4
5
6
7
|
<
select
class
=
"form-control"
name
=
"manufacturer"
id
=
"manufacturer"
>
<
option
></
option
>
<
option
selected
=
"selected"
>--请选择厂家--</
option
>
<
option
>HP</
option
>
<
option
>Huawei</
option
>
<
option
>Inspur</
option
>
</
select
>
|
pyhon 后端也是用request.values.get("name") 去获取
四 获取前端传输过来的json
request.json 返回的是一个字典
request.json.get("key",'')
curl模拟
curl -i -H "Content-Type: application/json" -X POST -d '{"title":"Read a book"}' http://localhost:5000/todo/api/v1.0/tasks
本文转自残剑博客51CTO博客,原文链接http://blog.51cto.com/cuidehua/1767207如需转载请自行联系原作者