django 獲取用戶提交的數據 文件 表單


templates:

<div>
    <form action="/detail" method="post" enctype="multipart/form-data">
        <p><span>性別=</span>
            男:<input type="radio" name="gender" value="male">
            女:<input type="radio" name="gender" value="female" checked="checked">
        </p>
        <p><span>愛好=</span>
            香蕉:<input type="checkbox" name="favor" value="banana">
            蘋果:<input type="checkbox" name="favor" value="apple">
        </p>
        <select name="area" multiple>
            <option value="bj">北京</option>
            <option value="sh">上海</option>
            <option value="gz">廣州</option>
        </select>
        <input type="file" name="file" />
        <input type="submit" value="提交" />
    </form>
</div>

views:

def detail(request):
    print(request.POST.get('gender'))
    print(request.POST.getlist('favor'))
    print(request.POST.getlist('area'))
    obj = request.FILES.get('file')  #上傳文件是用files獲取,是一個對象
    print(obj,type(obj))
    import os
    file_path = os.path.join('upload','1.png')
    f = open(file_path,'wb')
    for i in obj.chunks():   #chunks方法是一點點獲取上傳的文件內容
        f.write(i)
    f.close()
    return render(request,'detail.html')

  


免責聲明!

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



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