Create a folder to save the uploaded path
And then, in the setting, the path of the configuration file.
#配置文件的上传路径
Media_url= '/uploads '
media_root= os.path.join (base_dir, ' uploads ')
Then configure the user image upload route in the URL
Solve the picture of rich text editor long-passing problem:
The first thing to do is create a upload python file created under your app,
The specific code is as follows
1 #-*-coding:utf-8-*-2 fromDjango.httpImportHttpResponse3 fromDjango.confImportSettings4 fromDjango.views.decorators.csrfImportcsrf_exempt5 ImportOS6 ImportUUID7 ImportJSON8 Importdatetime as DT9 #This adorner is used to no longer form validation submissionsTen @csrf_exempt One defupload_image (Request, Dir_name): A ################## - #This is the format kindeditor want. - #kindeditor image upload return Data format description: the #{"Error": 1, "message": "Error Message"} - #{"Error": 0, "url": "Picture Address"} - ################## -result = {"Error": 1,"message":"Upload Error"} + #Imgfile from the Rich text editor after viewing the source code to find the file name it defines -Files = Request. Files.get ("Imgfile", None) + ifFiles: Aresult =image_upload (Files, dir_name) at returnHttpResponse (Json.dumps (result), content_type="Application/json") - - #Directory Creation - defUpload_generation_dir (dir_name): -Today =Dt.datetime.today () -Dir_name = Dir_name +'/%d/%d/'%(Today.year,today.month) in if notOs.path.exists (settings. Media_root +dir_name): -Os.makedirs (settings. Media_root +dir_name) to returnDir_name + - #image Upload the defimage_upload (Files, dir_name): * #allow file types to be uploaded $Allow_suffix =['jpg','PNG','JPEG','gif','BMP']Panax NotoginsengFile_suffix = Files.name.split (".") [-1] - ifFile_suffix not inchAllow_suffix: the return{"Error": 1,"message":"The picture is not formatted correctly"} +Relative_path_file =Upload_generation_dir (dir_name) APath=Os.path.join (settings. Media_root, Relative_path_file) the if notOs.path.exists (PATH):#Create directory If directory does not exist + os.makedirs (path) -File_name=str (UUID.UUID1 ()) +"."+File_suffix $path_file=os.path.join (Path, file_name) $File_url = settings. Media_url + Relative_path_file +file_name - #write operation, binary form, final upload, real save picture -Open (Path_file,'WB'). Write (Files.file.read ()) the return{"Error": 0,"URL": File_url}
View Code
Then we write the route inside the URL
Finally, we're going to write the path in the Rich text editor.
File Upload of Django development