0

I am trying to upload the images from django widget form. The rest of the form is filed correctly but the image field shows errors. Despite using the request.FILES is still getting the error. What could be the problem Here is my views.py file

def addProduct(request):
    form = ProductForm()
    if request.method == 'POST':
        form = ProductForm(request.POST, request.FILES)
        if form.is_valid():
            form.save()
            return redirect('modifyAllProduct')
    context = {
        'form': form
    }
    return render(request, 'products/Admin/addProduct.html', context)

Here is my form.py

class ProductForm(forms.ModelForm):
    class Meta:
        model = Product
        fields = ['image', 'title', 'category', 'price',
                           'description', 'is_published']
        widgets = {
            'image': forms.FileInput(attrs={'class': 'form-control'}),
            'title': forms.TextInput(attrs={'class': 'form-control'}),

            'category': forms.Select(attrs={'class': 'form-control'}),
            'price': forms.TextInput(attrs={'class': 'form-control'}),
            'description': forms.Textarea(attrs={'class': 'form-control'}),
            'is_published': forms.CheckboxInput(attrs={'class': 'form-control'}),

        }

Here is my model.py

class Product(models.Model):
    image = models.ImageField(null=False, blank=False)
    title = models.CharField(max_length=2000, null=False, blank=False)
    category = models.ForeignKey(
        Category, on_delete=models.CASCADE, default=True, null=False)
    price = models.DecimalField(max_digits=5, decimal_places=2)
    description = models.TextField()
    delivery_time = models.DateTimeField(default=datetime.now, blank=True)
    is_published = models.BooleanField(default=True)
    created_at = models.DateTimeField(default=datetime.now, blank=True)
    digital = models.BooleanField(default=False, null=True, blank=False)
2
  • Can you show your template Commented Aug 26, 2021 at 5:14
  • Thsnks, i was missing the enctype="multipart/form-data" Commented Aug 26, 2021 at 5:26

2 Answers 2

1

It seems that you have not provided this following code enctype="multipart/form-data" in your HTML form tag?

Sign up to request clarification or add additional context in comments.

Comments

0

Oh community, have just remembered i didn't put enctype="multipart/form-data" <form method="POST" action="" enctype="multipart/form-data"> Le it help someone

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.