def post(self, request): goodsTitle = request.POST.get("goodsTitle") price = request.POST.get("price") oprice = request.POST.get("oprice") category = int(request.POST.get("category")) address = request.POST.get("address") phone = request.POST.get("phone") description = request.POST.get("description") img1 = request.FILES.get("img1") img2 = request.FILES.get("img2") img3 = request.FILES.get("img3") img4 = request.FILES.get("img4") print category images = [] for img in [img1, img2, img3, img4]: if img is not None: images.append(img) if goodsTitle and price and address and phone and description and len( images) > 0: goods = Goods.objects.create(user=request.user, goods_title=goodsTitle, price=price, oprice=oprice, address=address, phone=phone, description=description, add_time=datetime.now(), category=category) goods.save() else: return render(request, "pubsuccess.html", { "status": 0, "msg": "发布失败!请按要求填写完整基本信息" }) for img in images: goodsImg = GoodsImage() goodsImg.goods = goods goodsImg.image = img goodsImg.add_time = datetime.now() goodsImg.save() return render(request, "pubsuccess.html", { "status": 1, "msg": "发布成功!" })
from db_tools.data.product_data import row_data from django.utils import timezone for goods_detail in row_data: goods = Goods() goods.name = goods_detail["name"] goods.market_price = float( int(goods_detail["market_price"].replace("¥", "").replace("元", ""))) goods.shop_price = float( int(goods_detail["sale_price"].replace("¥", "").replace("元", ""))) goods.goods_brief = goods_detail["desc"] if goods_detail[ "desc"] is not None else "" goods.goods_desc = goods_detail["goods_desc"] if goods_detail[ "goods_desc"] is not None else "" goods.goods_front_image = goods_detail["images"][0] if goods_detail[ "images"] else "" goods.add_time = timezone.now() category_name = goods_detail["categorys"][-1] category = GoodsCategory.objects.filter(name=category_name) if category: goods.category = category[0] goods.save() for goods_image in goods_detail["images"]: goods_image_instance = GoodsImage() goods_image_instance.image = goods_image goods_image_instance.goods = goods goods_image_instance.add_time = timezone.now() goods_image_instance.save()