Home > Net >  how to upload multiple images to a form with fields and two buttons to upload images?
how to upload multiple images to a form with fields and two buttons to upload images?

Time:03-08

I have a form divided into 3 tabs, in tab 2 and in tab 3 you have to upload multiple images respectively. The information that I have found about it, for the most part I do not understand and the one that I have seen easily, throws me this error:

TypeError: Carro() got an unexpected keyword argument 'images'

can you help me tell me what I am doing wrong? Thanks

models.py


class Carro(models.Model):
    placas=models.CharField(max_length=255)
    marca=models.CharField(max_length=255)
    cliente= models.ForeignKey(Clientes, on_delete=models.SET_NULL, null=True)
    fotosCarro=models.ImageField(null=True, upload_to="images/")
    garantia=models.ImageField(null=True, upload_to="images/")
    fecha_registros = models.DateTimeField(default=datetime.now, null=True)

    def __str__(self):
        return f'{self.placas} {self.marca}{self.cliente}{self.fotosCarro}{self.garantia}' \
               f'{self.fecha_registros}'

forms.py

class CarroForm(forms.ModelForm):
    class Meta:
        model=Carro
        fields = ['placas','marca','cliente','fotosCarro','garantia']
        exclude = ['fecha_registros']
        widgets = {
            'placas': forms.TextInput(
                attrs={

                    'class': 'form-control',

                }
            ),
            
            'marca': forms.TextInput(
                attrs={

                    'class': 'form-control'
                }
            ),
            
            'cliente': forms.Select(
                attrs={
                    'class': 'form-select'
                }
            ),
            'fotosCarro':forms.FileInput(
                attrs={
                    'class': 'type-file',
                    'multiple': True,
                    'id': 'file-input',
                    'onchange':'preview()',
                }
            ),
            'garantia':forms.FileInput(
                attrs={
                    'class': 'type-file',
                    'multiple': True,
                    'id': 'file-inputz',
                    'onchange': 'previewz()',
                    # 'id':'pro-images',
                    # 'click': "$('#pro-images').click()",
                }
            ),
            'fecha_registros': forms.DateInput(
                attrs={
                    'class': 'form-control',
                }
            ),

        }

views.py

def create_carros(request):
    form = CarroForm(request.POST or request.FILES)
    if request.method == "POST":
        form = CarroForm(request.POST or request.FILES)
        images = request.FILES.getlist('fotosCarro')
        garantias = request.FILES.getlist('garantia')
        for image in images:
            Carro.objects.create(images=image)
        for garantia in garantias:
            Carro.objects.create(garantias=garantia)


    images = Carro.objects.all()
    garantias = Carro.objects.all()
    return render(request, 'carros/carros-form-add.html', {'images': images,'garantias': garantias,'form':form})





CodePudding user response:

Change

Carro.objects.create(images=image)

to

Carro.objects.create(fotosCarro=image)

The keyword argument must match a field that you defined in the Carro class.

Note that this will fix the immediate error, but probably still won't do what you want. Also, you are doing WAY too much here. I suggest you read more about forms. If you use forms correctly, you can reduce your view code to this:

def create_carros(request):
    form = CarroForm(request.POST or request.FILES)
    if request.method == "POST":
        if form.is_valid():
            form.save()

You will also need to add error handling when the form is invalid and render the form with error messages.

  • Related