Djangoueditor is a pretty good Django Rich text editor that you can
Https://github.com/zhangfisher/DjangoUeditor
View related information.
I've been thinking about the Djangoueditor picture and file storage modified to seven cow cloud storage, but also feel that their JS level too dishes, so it has been delayed until now,
Actually I didn't change JS at all.
Okay, let's not talk nonsense, start.
One, the document uploaded to seven cattle
Before modifying Djangoueditor storage to seven kn, my first step was to upload a local file to seven cows.
This step requires an SDK of seven kn
Https://github.com/qiniu/python-sdk/releases
You can use
Install Qiniu
Installation.
1>>> Access_key ='your AK'2>>> Secret_key ='your SK'3>>> Bucket_name ='your bucket name'4 5>>> fromQiniuImportAuth,put_file6>>> q =Auth (Access_key, Secret_key)7>>> Postfile ='images/koala_20141220231309_308.jpg'8 9 #Postfile is the file and path that is represented after uploadingTen One>>> token =Q.upload_token (Bucket_name, Postfile) A>>> FilePath ='c:\\users\\r3call\\pictures\\zhuoku001.jpg' - - #FilePath to local file path the>>> ret, info =put_file (token, postfile, FilePath) ->>>ret -{u'Hash': U'Fnpm2sxt4_oixtvdg8bonbsniuad', u'Key'8 7'images/koala_20141220231309_308.jpg'}
Once the upload is successful, the RET dictionary will have a key called key, we can detect this key to determine whether the upload is successful.
Second, djangoueditor upload files to seven kn
Since the first step has been successful, we have the hope to upload files to seven kn, I first found the djangoueditor processing file upload function.
Here's the step I'm looking for:
1. Find urls.py
URL (r'^controller/$', Get_ueditor_controller)
2, so I know the function is called Get_ueditor_controller, then I find in the views.py
And then I found a function called UploadFile, which detects and stores file uploads.
which
State=save_upload_file (File,os.path.join (outputpath,outputfile))
is a function that really handles file storage, let's take a look at the code
1 #Save the uploaded file2 defSave_upload_file (postfile,filepath):3 Try:4f = open (FilePath,'WB')5 forChunkinchpostfile.chunks ():6 F.write (Chunk)7 exceptexception,e:8 f.close ()9 returnU"Write file Error:"+E.messageTen f.close () One returnU"SUCCESS"
I'm going to upload the file to seven cows and I need to follow this function to process the file.
So I wrote a function like this:
1 #Save upload file to seven kn2 defSave_upload_file_to_qiniu (upload_file,key):3Access_key =' Your AK'4Secret_key =' Your SK'5Bucket_name ='your bucket name'6 Try:7 fromQiniuImportAuth,put_file,put_data8Q =Auth (Access_key, Secret_key)9token =Q.upload_token (Bucket_name, key)Ten #ret, info = put_file (token, key, Upload_file) OneRET, info =Put_data (token, key, Upload_file) A ifRet.get ('Key', None) = =None: - RaiseException ('Upload Error') - Else: the returnU"SUCCESS" - exceptException, E: - Print(str (e)) - returnSTR (e)
After this function is written, where do I call it?
State=save_upload_file (File,os.path.join (outputpath,outputfile))
I'll change this to
State= Save_upload_file_to_qiniu (File,outputpathformat)
Where file is the uploaded image,
Outputpathformat is the file path that generates the picture, similar to the images/penguins_20141221001519_634.jpg
After the file is uploaded, call Save_upload_file_to_qiniu to store the file, and if successful, it will return SUCCESS, we return the result.
The original return information is as follows
1Return_info = {2 'URL': Urllib.basejoin (USettings.gSettings.MEDIA_URL, Outputpathformat),#the file name after saving3 'Original': Upload_file_name,#Original file name4 'type': Upload_original_ext,5 ' State': State,#upload Status, return success on success, any other value will be returned to the picture upload box6 'size': Upload_file_size7}
Before returning, we also need to define a variable
Qiniu_bucket_domain = ' http://hello.qiniudn.com/'
In this way, Qiniu_bucket_domain and Outputpathformat are connected, which is a full file URL address, similar to
Http://hello.qiniudn.com/images/Penguins_20141221001519_634.jpg
All we need to do is return this URL to the front end.
1 #Return Data2Qiniu_bucket_domain ='http://hello.qiniudn.com/'3Return_info = {4 #' URL ': Urllib.basejoin (USettings.gSettings.MEDIA_URL, Outputpathformat), # saved file name5 'URL': Urllib.basejoin (Qiniu_bucket_domain, Outputpathformat),#the file name after saving6 'Original': Upload_file_name,#Original file name7 'type': Upload_original_ext,8 ' State': State,#upload Status, return success on success, any other value will be returned to the picture upload box9 'size': Upload_file_sizeTen}
In this case, we have changed the storage of djangoueditor to seven cows and the basic end, if you need to manage files, or set AK and SK to the configuration file, you may have some work to do.
Note: The Doodle feature does not use the same file storage function, and if necessary, handle it separately.
Attached: modified views.py replacement Djangoueditor under the views.py can be, you need to fill out the Ak,sk,qiniu_bucket_domain
Need to install Qiniu SDK before use
Djangoueditor using seven Cow cloud storage