Django uploading file not in MEDIA_ROOT path is giving me SuspiciousOperation error

元气小坏坏 提交于 2019-12-03 16:21:05

问题


I want to upload files to a path that is still in my django project, but in my MEDIA_ROOT path.

When I try to do this I get a SuspiciousOperation error.

Here are the paths as defined in my settings file:

MEDIA_ROOT = os.path.join(os.path.dirname( __file__ ), 'static_serve')
UPLOAD_DIR = os.path.join(os.path.dirname( __file__ ), 'uploads')

I'm doing this because I don't want the files I am uploading to be accessible via the browser and my MEDIA_ROOT path is.

Does anyone have any idea how I get around (fix) this error.


回答1:


Yes there is a way:

From docs:

For example, the following code will store uploaded files under /media/photos regardless of what your MEDIA_ROOT setting is:

from django.db import models
from django.core.files.storage import FileSystemStorage

fs = FileSystemStorage(location='/media/photos')

class Car(models.Model):
    ...
    photo = models.ImageField(storage=fs)


来源:https://stackoverflow.com/questions/3631941/django-uploading-file-not-in-media-root-path-is-giving-me-suspiciousoperation-er

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!