Django學習手冊 - 前端input數據獲取


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:

 

測試獲取到的數值:

 

獲取的文檔:(在當前的目錄里面)

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM