我的部署脚本覆盖了媒体和源目录,这意味着我必须将上传目录移出媒体目录,并在提取上传后替换它。
如何指示 django 上传到 /uploads/ 而不是 /media/?
到目前为止,我不断收到 django 可疑操作错误!:(
我想另一种解决方案可能是符号链接?
非常感谢,托比。
我做了以下事情:
from django.core.files.storage import FileSystemStorage
upload_storage = FileSystemStorage(location=UPLOAD_ROOT, base_url='/uploads')
image = models.ImageField(upload_to='/images', storage=upload_storage)
UPLOAD_ROOT
在我的settings.py
文件中定义:/foo/bar/webfolder/uploads
虽然接受的答案可能是您想要的,但我们现在有了django
3.1 的选项,我们可以通过将函数传递给ImageField
or的 storage 参数来决定在运行时使用哪个存储FileField
。
def select_storage():
return MyLocalStorage() if settings.DEBUG else MyRemoteStorage()
class MyModel(models.Model):
my_file = models.FileField(storage=select_storage)
看看官方文档。
请注意,当前接受的答案会将变量的实际值写入UPLOAD_ROOT
迁移文件。当您将其应用于数据库时,这不会产生任何SQL
结果,但如果您经常更改该设置可能会造成混淆。