def edit_product(request,id): product=Product.objects.get(id=id) form=ProductForm(instance=product) if request.method=='POST': data = request.POST.copy() img_file=request.FILES.get('image') file_data= {'image':img_file} print data form=ProductForm(data,file_data) if form.is_valid(): simple_upload_file=SimpleUploadedFile(img_file.name,img_file.read()) img_file.seek(0) img=Image.open(img_file) img.thumbnail((128, 128)) outfile = StringIO() img.save(outfile,format="JPEG") product.name=data['name'] product.cost=data['cost'] product.desc=data['desc'] product.number=data['number'] product.image=simple_upload_file product.preview_image=SimpleUploadedFile(img_file.name,outfile.getvalue()) outfile.close() product.save() return HttpResponseRedirect('/admin/products/') return render_to_response('admin/product_form.html',{'product_form':form,'edit':True,'id':id},context_instance=RequestContext(request))
def add_product(request): form=ProductForm() if request.method=='POST': data = request.POST.copy() img_file=request.FILES.get('image') file_data= {'image':img_file} form=ProductForm(data,file_data) if form.is_valid(): simple_upload_file=SimpleUploadedFile(img_file.name,img_file.read()) img_file.seek(0) img=Image.open(img_file) img.thumbnail((128, 128)) outfile = StringIO() img.save(outfile,format="JPEG") product=Product(name=data['name'], number=data['number'], category=Category.objects.get(id=data['category']), cost=data['cost'],desc=data['desc'],image=simple_upload_file,preview_image=SimpleUploadedFile(img_file.name,outfile.getvalue())) outfile.close() product.save() #creating notification email params = {'pr_name': product.name, 'to': '*****@*****.**', 'type': 1} send_notification(params) return HttpResponseRedirect('/admin/products/') return render_to_response('admin/product_form.html',{'product_form':form,'edit':False},context_instance=RequestContext(request))