我正在编写一个管理命令来将文件上传到我的服务器。当文件位于项目文件夹中时,我能够正确地将文件上传到媒体。但我无法从文件夹外部上传文件。
这是文件在项目文件夹中时的版本:
def handle(self, *args, **options):
try:
log_module = LogModule.objects.get(customer__id=options['customer_id'])
except LogModule.DoesNotExist:
return
storage = log_module.storage_limit
log_objects = Log.objects.filter(module=log_module)
for log_object in log_objects:
storage -= (os.path.getsize('media/' + str(log_object.log_file)) / (1024 * 1024))
print('Available Storage: ' + str(storage))
if storage - os.path.getsize(options['file_path']) < 0:
my_file = open(options['file_path'], 'rb')
django_file = File(my_file)
log = Log(module=log_module)
log.log_file.save(django_file.name, django_file, save=True)
else:
print('Not Enough Storage Limit!!!')
return
这就是我想要实现的目标:
my_file = open(options['file_path'], 'rb')
django_file = File(my_file)
log = Log(module=log_module)
log.log_file.save(django_file.name, django_file, save=True)
当我尝试运行此管理命令时,出现以下错误:
Traceback (most recent call last):
File "./manage.py", line 15, in <module>
execute_from_command_line(sys.argv)
File "/home/hskl/.local/share/virtualenvs/ramtek_hotspot-fTktNGJ3/lib/python3.7/site-packages/django/core/management/__init__.py", line 381, in execute_from_command_line
utility.execute()
File "/home/hskl/.local/share/virtualenvs/ramtek_hotspot-fTktNGJ3/lib/python3.7/site-packages/django/core/management/__init__.py", line 375, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/home/hskl/.local/share/virtualenvs/ramtek_hotspot-fTktNGJ3/lib/python3.7/site-packages/django/core/management/base.py", line 323, in run_from_argv
self.execute(*args, **cmd_options)
File "/home/hskl/.local/share/virtualenvs/ramtek_hotspot-fTktNGJ3/lib/python3.7/site-packages/django/core/management/base.py", line 364, in execute
output = self.handle(*args, **options)
File "/home/hskl/Projects/ramtek_hotspot/hotspot/modules/log_module/management/commands/upload_log.py", line 43, in handle
log.log_file.save(django_file.name, django_file, save=True)
File "/home/hskl/.local/share/virtualenvs/ramtek_hotspot-fTktNGJ3/lib/python3.7/site-packages/django/db/models/fields/files.py", line 87, in save
self.name = self.storage.save(name, content, max_length=self.field.max_length)
File "/home/hskl/.local/share/virtualenvs/ramtek_hotspot-fTktNGJ3/lib/python3.7/site-packages/django/core/files/storage.py", line 51, in save
name = self.get_available_name(name, max_length=max_length)
File "/home/hskl/.local/share/virtualenvs/ramtek_hotspot-fTktNGJ3/lib/python3.7/site-packages/django/core/files/storage.py", line 75, in get_available_name
while self.exists(name) or (max_length and len(name) > max_length):
File "/home/hskl/.local/share/virtualenvs/ramtek_hotspot-fTktNGJ3/lib/python3.7/site-packages/django/core/files/storage.py", line 310, in exists
return os.path.exists(self.path(name))
File "/home/hskl/.local/share/virtualenvs/ramtek_hotspot-fTktNGJ3/lib/python3.7/site-packages/django/core/files/storage.py", line 323, in path
return safe_join(self.location, name)
File "/home/hskl/.local/share/virtualenvs/ramtek_hotspot-fTktNGJ3/lib/python3.7/site-packages/django/utils/_os.py", line 46, in safe_join
'component ({})'.format(final_path, base_path))
django.core.exceptions.SuspiciousFileOperation: The joined path (/home/hskl/Downloads/network02.zip) is located outside of the base path component (/home/hskl/Projects/ramtek_hotspot/media)
``