我正在使用Django创建一个电子商务Web应用程序,但我坚持将多个图像添加到一个产品中。
这是我的Forms.py
class ProductForm(forms.ModelForm):
images = forms.ImageField(widget=forms.ClearableFileInput(attrs={'multiple': True}))
class Meta():
model = Products
labels = {
"isbn": "ISBN"
}
fields = ('isbn', 'title', 'authors', 'publication_date', 'quantity', 'price', 'images')
widgets = {
'publication_date': DateInput()
}
这是我的Models.py
class Products(models.Model):
isbn = models.CharField(max_length=50)
title = models.CharField(max_length=264)
authors = models.CharField(max_length=264)
publication_date = models.DateField(max_length=264)
quantity = models.DecimalField(max_digits=3, decimal_places=0)
price = models.DecimalField(max_digits=6, decimal_places=2)
seller = models.ForeignKey(User, on_delete=models.CASCADE)
creation_date = models.DateTimeField(auto_now_add=True)
updation_date = models.DateTimeField(auto_now=True)
images = models.ImageField(upload_to='images/',null=True,blank=True)
def __str__(self):
return self.title
def get_absolute_url(self):
return reverse("book_item", kwargs={'pk': self.pk})
def get_add_to_cart_url(self):
return reverse("add-to-cart", kwargs={
'pk': self.pk
})
def get_remove_from_cart_url(self):
return reverse("remove-from-cart", kwargs={
'pk': self.pk
})
这是我的创建视图
class ProductCreate(LoginRequiredMixin, SuccessMessageMixin, CreateView, ):
form_class = ProductForm
model = Products
template_name = 'books/new_book.html'
success_message = "Books Added"
def form_valid(self, form):
form.instance.seller = self.request.user
Products = form.save()
Products.save()
return super().form_valid(form)
我想在我的产品中添加多张图片。