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" \>
</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:

Django学习手册 - 前端input数据获取

 

测试获取到的数值:

Django学习手册 - 前端input数据获取

 

获取的文档:(在当前的目录里面)

Django学习手册 - 前端input数据获取

 

相关文章:

  • 2022-01-01
  • 2022-01-10
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-08-18
猜你喜欢
  • 2022-12-23
  • 2022-01-19
  • 2022-12-23
  • 1970-01-01
  • 2022-12-23
  • 2021-10-20
  • 2021-07-08
相关资源
相似解决方案