Django models 无法访问UpdateView中的ModelForm images字段以更新Django中的图像(已解决)

Django models 无法访问UpdateView中的ModelForm images字段以更新Django中的图像(已解决),django-models,django-views,django-forms,django-class-based-views,imagefield,Django Models,Django Views,Django Forms,Django Class Based Views,Imagefield,我正在尝试为一个产品更新多个图像。我已经清除了预先存在的图像,但无法访问从ModelForm传递的新图像 我的模型: def get_image_path(instance, filename): name = instance.title return f'{name}/{filename}' def get_file_path(instance, filename): name = instance.title return f'{name}/files/{

我正在尝试为一个产品更新多个图像。我已经清除了预先存在的图像,但无法访问从ModelForm传递的新图像

我的模型:

def get_image_path(instance, filename):
    name = instance.title
    return f'{name}/{filename}'

def get_file_path(instance, filename):
    name = instance.title
    return f'{name}/files/{filename}'

def get_upload_path(instance, filename):
    name = instance.product.title
    return f'{name}/images/{filename}'

class ProductImage(models.Model):
    product = models.ForeignKey('Product', on_delete=models.CASCADE)
    image = models.ImageField(upload_to=get_upload_path, blank=True, null=True)

    def __str__(self):
        return self.product.title

class Product(models.Model):
    title = models.CharField(max_length= 100)
    price = models.DecimalField(decimal_places=2, max_digits=9, default=0.00)
    description = models.TextField()
    image = models.ImageField(upload_to=get_image_path, blank=True,  null=True)
    video = models.FileField(upload_to=get_file_path, validators=[validate_file_extension], blank=True, null=True)
    discount_price = models.DecimalField(decimal_places=2, max_digits=8, default=0.00)
    date_created = models.DateTimeField(auto_now_add=True)
    date_updated = models.DateTimeField(auto_now=True)

    slug = models.SlugField(max_length=100, unique=True)

    supplier = models.ForeignKey(USER, on_delete=models.SET_NULL, null=True)

    season_choice = models.CharField(choices=SEASON_CHOICES, max_length=10, default='summer')
    tags = models.ManyToManyField(Tag, blank=True, related_name='products')
    

    def __str__(self):     
        return self.title
我的模型:

class ProductCreateForm(forms.ModelForm):
    more_images = forms.FileField(required=False, widget=forms.FileInput(attrs={
        "class": "form-control",
        "multiple": True
    }))

    class Meta:
        model = Product
        fields = ['title', 'price', 'description', 'image', 'video', 'discount_price',
                    'season_choice', 'tags']
我的更新视图

class ProductUpdateView(LoginRequiredMixin, UserPassesTestMixin, UpdateView):
    model = Product
    form_class = ProductCreateForm
    template_name = 'product/product_update.html'
    success_url = reverse_lazy('home')

   
    def form_valid(self, form):
        form.instance.supplier = self.request.user               

        product = self.get_object() 
        
        # Clear existing images/This is working
        for post_image in ProductImage.objects.filter(product=product):
            post_image.delete()

        # Updating new images/This is not working
        images = self.request.FILES.getlist('more_images')
        print(images)
        
        for i in images:
            ProductImage.objects.create(product=product, image=i)

        form.save()
        return super().form_valid(form)

如何从CBV中form_valid()方法内的ModelForm访问在ImageField中传递的图像。我的代码提供了空查询集和空列表。

我的代码完全正常,问题在我的模板中。我忘记在没有提交媒体文件的表单标签bcoz中添加enctype。