Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/eclipse/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Django-[Errno 2]无此类文件或目录错误:尝试将上载的文件保存到动态url时_Django_Django File Upload - Fatal编程技术网

Django-[Errno 2]无此类文件或目录错误:尝试将上载的文件保存到动态url时

Django-[Errno 2]无此类文件或目录错误:尝试将上载的文件保存到动态url时,django,django-file-upload,Django,Django File Upload,Models.py: class Enterprise(models.Model): name = models.CharField(max_length = 100) def __str__(self): return f"{self.id}_{self.name}" class Client(models.Model): name = models.CharField(max_length = 100) def __str

Models.py:

class Enterprise(models.Model):
    name = models.CharField(max_length = 100)
    def __str__(self):
        return f"{self.id}_{self.name}"

class Client(models.Model):
    name = models.CharField(max_length = 100)
    def __str__(self):
        return f"{self.id}_{self.name}"    

class Engagement(models.Model):
    name = models.CharField(max_length = 100    
    def __str__(self):
        return f"{self.id}_{self.name}"

class StockCount(models.Model):
    name = models.CharField(max_length = 100)    
    def __str__(self):
        return f"{self.name}"

class InventoryList(models.Model):
    UploadedFile = models.FileField(_('Upload Inventory Listing'), upload_to = file_upload_location, validators=[validate_file_extension], max_length = 500)
    enterprise = models.ForeignKey('Enterprise', on_delete=models.CASCADE, related_name = 'inventorylists')
    client = models.ForeignKey('Client', on_delete=models.CASCADE, related_name = 'inventorylists')
    engagement = models.ForeignKey('Engagement', on_delete=models.CASCADE, related_name = 'inventorylists')
    stockcount = models.ForeignKey('StockCount', on_delete=models.CASCADE, related_name = 'inventorylists')
views.py:

def upload(request):   
    if request.method == 'POST':
        form = InventoryListForm(request.POST, request.FILES)
        if form.is_valid():    
            # file is saved
            list = form.save(commit = False)
            list.enterprise = Enterprise.objects.get(pk = 1)
            list.client = Client.objects.get(pk = 1)
            list.engagement = Engagement.objects.get(pk = 1) 
            list.stockcount = StockCount.objects.get(pk = 1)   
            list.save() 
            return HttpResponse(f'You have just made a post request - {list.id}')   
        else:
            return render(request, "observe/upload.html", {"form": form})
    else: 
        return render(request, "observe/upload.html", {"form": InventoryListForm()})
forms.py:

class InventoryListForm(ModelForm):
    class Meta:
        model = InventoryList
        exclude = ['enterprise', 'client', 'engagement', 'stockcount']
    def __init__(self, *args, **kwargs):
        super(InventoryListForm, self).__init__(*args, **kwargs)
将_上载到可调用函数:

def file_upload_location(instance, filename):
    ext = filename.split('.')[-1]
    # return f"{instance.enterprise}/{instance.client}/{instance.engagement}/{instance.stockcount}/{filename}"
    # return f"{filename}"
    FileType = '\\Inventory List'
    name = str(filename)
    path = os.path.join(str(instance.enterprise), str(instance.client), str(instance.engagement), str(instance.stockcount))
    # return f"{path}/{filename}"
    # return path 
    print(f"The path is {path}")
    # return f"{path}/"
    # return '{0}/{1}/{2}/{3}/{4}'.format(str(instance.enterprise), str(instance.client), str(instance.engagement), str(instance.stockcount), filename)
    return os.path.join("%s" % str(instance.enterprise), "%s" % str(instance.client), "%s" % str(instance.engagement), "%s" % str(instance.stockcount), filename)
我已经尝试了可调用函数的多种变体(正如上面从函数的注释部分可以看到的),但是我仍然得到了这个错误。有趣的是,当我第一次上传文件并提交表单时,它就工作了。然而,当我第二次尝试时,它给了我这个errno2错误

完整回溯如下所示:

[Errno 2]没有这样的文件或目录:“C:\Users\bilal\Desktop\Inventory Observation Mobile Responsive Web Application\Inventory Observation Mobile Responsive Web Application\Inventory Observation\files\1\u RSM Canada\PK752\u Nuvera Corp\FY 2021\u招股说明书工作\London Count\Monthly\u Sales\u Reporting\u Template\u Tool\u Start.xlsm”

似乎我的实际解决方案适用于某些excel文件,而不适用于其他excel文件,并且根据对excel文件的进一步检查,我能够成功上载的excel文件与我无法上载的excel文件似乎没有任何不同。

尝试使用以下方法:-

  UploadedFile = models.FileField(_('Upload Inventory Listing'), upload_to = 'file_upload_location', validators=[validate_file_extension], max_length = 500)
而不是:-

  UploadedFile = models.FileField(_('Upload Inventory Listing'), upload_to = file_upload_location, validators=[validate_file_extension], max_length = 500)
我编辑的内容:-我在“文件上传位置”中添加了COMA

试着使用它。

试着使用它:-

  UploadedFile = models.FileField(_('Upload Inventory Listing'), upload_to = 'file_upload_location', validators=[validate_file_extension], max_length = 500)
而不是:-

  UploadedFile = models.FileField(_('Upload Inventory Listing'), upload_to = file_upload_location, validators=[validate_file_extension], max_length = 500)
我编辑的内容:-我在“文件上传位置”中添加了COMA


尝试使用它。

谢谢您的回复,但不幸的是,这不起作用。您建议的解决方案基本上最终创建了一个名为“文件到上传”的文件夹。感谢您的回复,但不幸的是,这没有起作用。您建议的解决方案基本上是创建一个名为“文件到上传”的文件夹。