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)