def process_item(self, item, spider): p = Product() p.title = item["title"] p.price = float(item['price'].replace(',', '.').strip()) p.image = item['image_url'] or '' p.save() return item
def add_product(request): context = {} form = ProductForm(request.POST, request.FILES) context['form'] = form if form.is_valid(): datas = form.cleaned_data name, price, city = datas['name'], datas['price'], datas['city'] quantity, availability = datas['quantity'], datas['availability'] description, pictures = datas['description'], request.FILES.getlist( 'pictures') category = datas['category'] product = Product(name=name, price=price, city=city, quantity=quantity, \ description=description, availability=availability) product.slug = slugify(product.name) product.save() # On ajoute les catégories. import karim.functions as f categories = f.parse_select_multiple(category) for cat in categories: _cat = get_object_or_404(Category, name=cat) product.category.add(_cat) # On ajoute les images. for key, img in enumerate(pictures): if key == 0: product.image = "products/" + str(img) product.save() pic = Picture(product=product, picture=img) pic.save() f.handle_upload_file("products", str(img), img) return render(request, "add_product.html", context)