view获取数据方法归纳:
#请求meta数据
request.mata(...)
request.method(POST,GET,PUT)
#从meta里面获取数据
request.path_info
request.COOKIES #请求body数据
request.POST(从body里面获取数据)
request.FILES(从body里面获取数据)
request.GET
request.xxx.getlist
请求body数据(以下都是请求body数据)
前端页面代码:(整个复制即可)
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<style>
body{
margin: 0;
}
.body_div{
position: absolute;
left: 40%;
top: 10%;
}
label{
display: inline;
}
</style>
</head>
<body>
<div class="head_div">
</div>
<div class=" body_div">
<h2>
前端input控件 提交数据至后台
</h2>
<br>
<form action="/index/" method="post" enctype="multipart/form-data">
<p>text框:
<label>text框:<input type="text" name="text_1" \></label>
</p>
<p>密码框:
<label>pwd框:<input type="password" name="pwd_2" \></label>
</p>
<br>
<p>单选框:
<label for="r1">radio框1:</label><input value="1" type="radio" name="radio_3" id="r1" \>
<label for="r2">radio框2:</label><input value="2" type="radio" name="radio_3" id="r2" \>
<label for="r3">radio框3:</label><input value="3" type="radio" name="radio_3" id="r3"\>
</p>
<p>多选框:
<label for="c1">checkbox框1:</label><input type="checkbox" value="1" name="checkbox_4" id="c1" \>
<label for="c2">checkbox框2:</label><input type="checkbox" value="2" name="checkbox_4" id="c2" \>
<label for="c3">checkbox框3:</label><input type="checkbox" value="3" name="checkbox_4" id="c3"\>
</p>
<br>
<p>单选下拉框:
<select name="select_5">
<option value="A">A</option>
<option value="B">B</option>
<option value="C">C</option>
<option value="D">D</option>
<option value="E">E</option>
</select>
</p>
<p>多选下拉框:
<select name="select_6" multiple="multiple">
<option value="AA">AA</option>
<option value="BB">BB</option>
<option value="CC">CC</option>
<option value="DD">DD</option>
<option value="EE">EE</option>
</select>
</p>
<br>
<p>文本上传框:
<input type="file" name="file_7" \>
</p>
<input type="submit" value="提交" \>
</form>
</div>
</body>
</html>
前端提交给后台数据是根据 name 参数 传递的,传递的数值 是value 属性。
text框:name= text_1
pwd框:name= pwd_2
radio框:name= radio_3
checkbox框:name=checkbox_4
select下拉框: name=select_5
select 多选下拉框:name=select_6
file文件上传: name=file_7
后端views python代码:
from django.shortcuts import render
# Create your views here.
def index(request):
if request.method == "POST":
t1 = request.POST.get("text_1",None)
t2 = request.POST.get("pwd_2",None)
t3 = request.POST.get("radio_3",None)
t4 = request.POST.getlist("checkbox_4",None)
t5 = request.POST.get("select_5",None)
t6 = request.POST.getlist("select_6",None)
# t7 = request.POST.get("file_7")
t7 = request.FILES.get("file_7")
print(
"text:",t1,'-----',
"password",t2,'-----',
"radio",t3,'-----',
"checkbox",t4,'-----',
"select_单选",t5,'-----',
"select_多选",t6,'-----',
"文件上传",t7.name
)
f = open(t7.name, mode="wb")
for i in t7.chunks():
f.write(i)
f.close()
return render(request,"index.html")
给定目录保存文件:(注意1.必须创建给定文件,2.当前目录存在时,目录文件不需要加/ )
t7 = request.FILES.get("file_7")
file_path = ''.join(('static/',t7.name))
f = open(file_path, mode="wb")
for i in t7.chunks():
f.write(i)
f.close()
配置URL:
测试获取到的数值:
获取的文档:(在当前的目录里面)