def goods_upload(request): r = json.loads(request.body) title = r['title'] detail = r['detail'] category = r['category'] # {url: url1, hash: hash1} # url通过 | 分割多个图片链接存在一个字段,hash同理 url_array = r['url'] seller_id = r['sellerId'] price = r['price'] express = r['express'] area = r['area'] url = '' for item in url_array: url = url + str(item['url']) + '|' img_hash = '' for item in url_array: img_hash = img_hash + str(item['hash']) + '|' goods = Goods() goods.status = 2 goods.title = title goods.detail = detail goods.category = category # 去除最后多余的| goods.url = url[:-1] goods.img_hash = img_hash[:-1] goods.seller_id = seller_id goods.price = price goods.express = express # 去除最后多余的, goods.area = area[:-1] goods.save() return JsonResponse({'success': True})
def post(self, request, page): """处理商品上架""" # 接收数据 name = request.POST.get('name') price = request.POST.get('price') stock = request.POST.get('stock') pack = request.POST.get('pack') typ = request.POST.get('type') img = request.FILES.get('file') # 校验基本数据 if not all([img, name, price, stock, pack, typ]): # 数据不完整 return render(request, 'sj_cpgl.html', {'errmsg': '缺少相关数据'}) # FDFS上传图片 rec = FastDFSStorage().save(img.name, img) # 业务表添加数据 try: df_shop = Shop.objects.get(user_id=request.user.id) except Shop.DoesNotExist: return render(request, 'login.html', {'errmsg': '用户登录信息已失效,请重新登录!'}) df_goods = Goods(name=name, shop_id=df_shop.id) df_goods.save() # 确定是否为新增类型 try: df_goods_type = GoodsType.objects.get(name=typ, shop_id=df_shop.id) except GoodsType.DoesNotExist: df_goods_type = GoodsType(name=typ, shop_id=df_shop.id) df_goods_type.save() df_goods_sku = GoodsSKU(goods=df_goods, type=df_goods_type, name=name, price=price, unite='per', stock=stock, pack=pack) df_goods_sku.save() df_goods_image = GoodsImage(image=rec, sku_id=df_goods_sku.id) df_goods_image.save() return redirect(reverse('goodsmanage:sj_cpgl', kwargs={'page': 1}))
django.setup() from apps.goods.models import Goods, GoodsCategory, GoodsImage from db_tools.data.my_product_data import row_data for goods_detail in row_data: goods = Goods() goods.name = goods_detail["name"] goods.price = float( int(goods_detail["price"].replace("¥", "").replace("元", ""))) goods.goods_intro = goods_detail["desc"] if goods_detail[ "desc"] is not None else "" goods.goods_front_img = goods_detail["images"][0] if goods_detail[ "images"] else "" goods.goods_sn = hashlib.md5( (str(int(time.time())) + goods.name).encode('utf-8')).hexdigest() 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.save()
def post(self, request): # 获取数据 user = User.objects.filter(id=request.user.id)[0] title = request.POST.get('title') province = request.POST.get('province') city = request.POST.get('city') district = request.POST.get('district') gtype = request.POST.get('gtype') detail = request.POST.get('gcontent') price = request.POST.get('price') con = request.POST.get('condition') files = request.FILES.getlist('files') fineness = [] for fin in Goods.comm_quality: fineness.append({'id': fin[0], 'value': fin[1]}) context = { 'fin': fineness, 'gtype': GoodsType.objects.values('id', 'name'), 'msg': '' } # 数据检查 if not all([ gtype, title, province, city, district, detail, price, con, files ]): context['msg'] = "数据填写不完整,每个都为必填项!" return render(request, 'good.html', context) if len(title) > 256: context['msg'] = "标题过长!不能超过256个字符" return render(request, 'good.html', context) if float(price) > 99999.00 or float(price) < 0.00: context['msg'] = "价格输入有误(0~99999)!" return render(request, 'good.html', context) gtype = int(gtype) price = float(price) con = int(con) title.strip() city.strip() province.strip() district.strip() addr = ' '.join([province, city, district]) up_date = str(datetime.datetime.now(tz=timezone.utc)) # 保存商品信息 try: gtype = GoodsType.objects.get(id=gtype, is_delete=0) except Exception: context['msg'] = "类型选择错误!" return render(request, 'good.html', context) good = Goods(type=gtype, user=user, title=title, price=price, detail=detail, addr=addr, fineness=con, update_time=up_date) good.save() # 上传文件 fdfs = FDFSStorage() for file in files: try: url = fdfs.save(name=file.name, content=file) GoodsImage(goods=good, image=url).save() except Exception: GoodsImage(goods=good).save() return redirect(reverse('goods:good_list'))