【发布时间】:2018-07-31 21:16:29
【问题描述】:
在我的 Django 项目中,我有一个应用程序,我想在其中加载不在 MEDIA_ROOT 中的文件。我使用storage 属性更改了位置,但它引发了错误。
我使用了下一个代码,但是当我尝试加载文件时它会引发错误。我该如何解决这个问题?
settings.py:
MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'media_root')
models.py:
from django.core.files.storage import FileSystemStorage
from os import environ
PRODUCT_STORAGE = FileSystemStorage(location=environ.get('PRODUCT_STORAGE_PATH'))
def product_file_upload_path(instance, filename):
if instance.category=="1":
path = '/category_1/' + '/%s' % filename
return path
elif instance.category=="2":
path = '/category_2/' + '%s' % filename
return path
else:
path = '%s' % filename
return path
class Product(models.Model):
file = models.FileField(
max_length=255,
blank=True,
null=True,
validators=[validate_file_extension],
storage=PRODUCT_STORAGE,
upload_to=product_file_upload_path,
)
错误:
The joined path (/category_1/test.pdf) is located outside of the base path component (/other_folder)
【问题讨论】:
-
我已经发现了问题。正如你所说,问题出在斜杠上。无论如何,您可以将您的答案发布为帖子(而不是评论),我会标记它,兄弟;)
标签: python django python-2.7 django-1.11