我有一个页面,您可以在其中添加产品,添加缩略图以及为产品添加图像。
我正在使用formsets,如下所示。问题是上传到图像formset的图像是否正确上传,但上传到缩略图formset的图像根本没有上传...我可能做错了什么?
def AddProduct(request):
ImageFormSet = modelformset_factory(ProductImage,
form=ImageForm, extra=4)
ThumbnailFormSet = modelformset_factory(ProductvThumbnail,
form=ThumbnailForm)
if request.method == 'POST':
product_form = AddEditProductForm(request.POST)
image_formset = ImageFormSet(request.POST, request.FILES, prefix='images',
queryset=roductImage.objects.none())
thumbnail_formset = ThumbnailFormSet(request.POST, request.FILES, prefix='thumbnail',
queryset=ProductThumbnail.objects.none())
if product_form.is_valid() and image_formset.is_valid() and thumbnail_formset.is_valid():
product = product_form.save()
thumbnails = thumbnail_formset.save(commit=False)
for thumbnail in thumbnails:
thumbnail.product = product
thumbnail.save()
images = image_formset.save(commit=False)
for image in images:
image.product = product
image.save()
return HttpResponseRedirect('/product/')
else:
print (product_form.errors, image_formset.errors, thumbnail_formset.errors)
else:
product_form = AddEditProductForm()
image_formset = ImageFormSet(queryset=ProductImage.objects.none(), prefix='images')
thumbnail_formset = ThumbnailFormSet(queryset=ProductThumbnail.objects.none(), prefix='thumbnail')
return render(request, 'product/add.html',
{'product_form': product_form, 'image_formset': image_formset,
'thumbnail_formset': thumbnail_formset},
context_instance=RequestContext(request))
答案 0 :(得分:0)
我找到了一个解决方案 - 我的formset看起来很好,问题出在我的models.py中。 ProductThumbnail模型中的ImageField被称为'image = models.ImageField ...'但它应该被称为缩略图 - 这就是没有上传缩略图的原因 - 我的for循环没有找到任何东西。