Django 中的错误 - [WinError 3] 系统找不到指定的路径 - Django

问题描述 投票:0回答:1

问题是:我试图让用户上传 5 张图像,并且在上传时我希望他们更改目录。

到目前为止我的观点.py

class ProductCreateView(LoginRequiredMixin, CreateView):
    template_name = "product/add_product.html"
    success_url = reverse_lazy('home')
    form_class = ProductForm
    login_url = "/login/"


    # Save current user id as product_user_id in DB

    def form_valid(self, form, **kwargs):

        form.instance.product_user_id = self.request.user.id
        response = super().form_valid(form)

        id = form.instance.id
        picture_1 = form.instance.product_img_1
        picture_2 = form.instance.product_img_2
        picture_3 = form.instance.product_img_3
        picture_4 = form.instance.product_img_4
        picture_5 = form.instance.product_img_5
        product_title = form.instance.product_title
        user = form.instance.product_user_id

        # Change directory

        new_directory = change_directory(id, product_title, user, picture_1, picture_2, picture_3, picture_4, picture_5)

        return new_directory

utils.py

def change_directory(id, product_title, user, picture_1, picture_2, picture_3, picture_4, picture_5):

    file_name = f"{id}-{product_title}-{product_title}-{user}"
    new_path = os.path.join(settings.MEDIA_ROOT, "product_images", file_name)

    if not os.path.exists(new_path):
        os.makedirs(new_path)

    picture_list = [picture_1, picture_2, picture_3, picture_4, picture_5]

    for i in picture_list:
        old_image_path = os.path.join(settings.MEDIA_ROOT, f"product_images/{i}")
        new_image_path = os.path.join(settings.MEDIA_ROOT, f"product_images/{file_name}/{i}")
        os.rename(old_image_path, new_image_path)               

    return new_path

解决方案并不遥远,但我没有想法。

如果这有帮助:

模型.py

class Product(models.Model):
    product_title = models.CharField(max_length=255)
    product_description = models.TextField()
    product_description_short = models.CharField(max_length=255, blank=True)
    product_condition = models.CharField(max_length=100)
    product_code = models.CharField(max_length=100, blank=True)
    product_manufacturer = models.CharField(max_length=100, blank=True)
    product_location_state = models.CharField(max_length=100)
    product_location_city = models.CharField(max_length=100)
    product_delivery_time = models.IntegerField()
    product_category = models.ForeignKey(ProductCategory, on_delete=models.CASCADE, related_name='products')
    product_img_1 = models.ImageField(upload_to="product_images", blank=True)
    product_img_2 = models.ImageField(upload_to="product_images", blank=True)
    product_img_3 = models.ImageField(upload_to="product_images", blank=True)
    product_img_4 = models.ImageField(upload_to="product_images", blank=True)
    product_img_5 = models.ImageField(upload_to="product_images", blank=True)
    price = models.DecimalField(max_digits=10, decimal_places=2, default=0.0)
    product_user = models.ForeignKey(User, on_delete=models.CASCADE, related_name='user')
    created_at = models.DateTimeField(auto_now_add=True)

    def __str__(self):
        return self.product_title

表格.py

class ProductForm(ModelForm):

    class Meta:
        model = Product
        fields = [
        'product_title',
        'product_description',
        'product_description_short',
        'product_condition',
        'product_code',
        'product_manufacturer',
        'product_location_state',
        'product_location_city',
        'product_delivery_time',
        'product_category',
        'product_img_1',
        'product_img_2',
        'product_img_3',
        'product_img_4',
        'product_img_5',
        'price'
        ]

错误:[WinError 3]系统找不到指定的路径:'C:\Users ener\OneDrive\Desktop\Web 开发人员 bootcamp\Enda projektid\Ecom\project-ecom com\media\product_images/product_images/DSC_0922_yhSMaeD.JPG' -> 'C:\Users ener\OneDrive\Desktop\Web 开发人员训练营\Enda projektid\Ecom\project-ecom com\media\product_images/15-Autouus-Autouus-2/product_images/DSC_0922_yhSMaeD.JPG'

我希望它首先上传到product_images文件夹,然后将它们移动到product_images/{file_name}文件夹。

python django directory
1个回答
0
投票

花了一些时间从互联网上搜索后,我发现了一些不同的将图像保存到与根目录不同的文件夹的方法。这是更快、更简单的方法,但不幸的是,使用这种方法您将无法访问产品的 id。

据我所知,在保存之前无法获取产品ID,但可以访问其他字段。

© www.soinside.com 2019 - 2024. All rights reserved.