This series focuses on operability and introduces how to implement some simple functions through the djangoweb framework. Each article has integrity and independence. Beginners can understand the web development process in the hands-on process. For details, see relevant documents. 1. File Upload (input tag)
(1) html code (form is submitted using the post method)
(2) Submit the jq form to the background
$ ("# SubmitForm "). click (function () {// alert ($ ("# SelectBus "). val (); addNameForm (); // because the content of the dynamically loaded form is used, the name value of the tag given by the function is $. ajaxSetup ({async: false}); $ ("# picture_form "). ajaxSubmit ({resetForm: false, dataType: 'json', success: function (data) {if (data = 1) {alert ("submitted successfully ");} else {alert ("failed to submit ");}}});});
(3) The python background processes the content transmitted by the form, mainly for file processing.
# Custom storage path rollfileName = "webStatic/uploadfile/files/" rollfilePath = OS. path. join (basePath, rollfileName) # req. POST. get (text [1], '') if the information is obtained, the value is not 123. if the information is null, the result is 123 if req. POST. get (text [1], '000000') = '000000': # obtain the binary stream reqfile = req. FILES [text [1] # obtain the filename suffix filetype = reqfile. name. split (". ") [-1] # generate a random string with a suffix filename = str (uuid. uuid1 () + '. '+ filetype # open the file storage path of = open (rollfilePath + filename, 'wb +') # Write the file for chunk in reqfile to the specified path. chunks ():. write (chunk) # write content. close () # close the connection
18 # Storage path rollfileName + filename in the database
(4) packages used for python background processing
1 # generate an unordered string and replace the file name
2 import uuid