Submission based on form form forms
# You need to specify Form-data, you cannot directly spell-key-value pairs
You can specify where the name photo exists
views.py
fromDjango.shortcutsImportRender,redirect,httpresponse#Create your views here.ImportOSdefIndex (Request):ifrequest.method=="POST": Print(Request. POST)#only key value pairs are placed in post #< querydict: {' Csrfmiddlewaretoken ':#[' hs5bynikbnqazl7svwkxwrjxxy5sftir1mxrmumqox9dizs4bwz7nn183um8h0u8 '], ' user ': [' 6666 '], ' f ': [' vzh.jpg ']} > Print(Request. FILES)#<multivaluedict: {' F ': [<inmemoryuploadedfile:vzh.jpg (image/jpeg) >]}>obj = Request. Files.get ("F")#remove the object that uploaded the fileName = Obj.name#the name of the specific fileF_write = open (Os.path.join ("APP01","Static","img", name),"WB")#If you want to place the file in a specific location, stitch the path together, plus the name #If you put only name, then the file will exist under the current root directory forLineinchObj:#Take out data for each rowF_write.write (line)returnHttpResponse ("111") returnRender (Request,"index.html")defput (Request):returnHttpResponse ("OK")
<! DOCTYPE html>"en">"UTF-8"> <meta http-equiv="x-ua-compatible"Content="Ie=edge"> <meta name="Viewport"Content="Width=device-width, initial-scale=1"> <title>Title</title>{#form-data type can upload file #}<form action=""Method="Post"Enctype="Multipart/form-data"> {% Csrf_token%} <p><input type="text"Name="User"></p> <p><input type="file"Name="F"></p> <input type="Submit"></form></body>index.htmlNote the point:
Form form <form action= "" method= "post" enctype=" Multipart/form-data"> need to add enctype="multipart/form-data "form-data can be used to upload files
Python File Upload