定义:用户可以通过浏览器将图片等文件传至网站
场景:用户上传头像,上传流畅性的文档[PDF,txt等]
文件上传必须为POST提交方式
表单<form>中文件上传时必须带有enctype = “multipart/form-data”时才会包含文件内容数据
表单中用<input type = 'file' name = 'xxx'>标签上传文件
#views.py代码:
def test_upload(request):
if request.method == 'GET':
return render(request,'test_upload.html',locals())
elif request.method == 'POST':
return HttpResponse('test_upload is ok!')
#urls.py中urlpatterns添加path('test_upload/',views.test_upload),
#text_upload代码:
<body>
<form action="/music/test_upload/" method="post" enctype="multipart/form-data">
<p>
<input type="text" name="title">
</p>
<p>
<input type="file" name="myfile">