Django: How to allow a Suspicious File Operation / copy a file

后端 未结 4 567
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-12-04 01:54

I want to do a SuspiciousFileOperation which django disallows by default.

I am writing a command (to run via manage.py importfiles) to impo

4条回答
  •  长情又很酷
    2020-12-04 02:55

    In Django, SuspiciousFileOperation can be avoid by read the file from external dir and make a tmp file within the project media then save in the appropriate file filed as below

    import tempfile
    
    file_name="file_name.pdf"
    EXT_FILE_PATH = "/home/somepath/"
    file_path = EXT_FILE_PATH + file_name
    if exists(file_path):
        #create a named temporary file within the project base , here in media
    
        lf = tempfile.NamedTemporaryFile(dir='media')
        f = open(file_path, 'rb')
        lf.write(f.read())
        #doc object with file FileField.
    
        doc.file.save(file_name, File(lf), save=True)
        lf.close()
    

提交回复
热议问题