Esempio n. 1
0
def goods_change(request, id):
    doType = "change"
    goods = Goods.objects.get(id=int(id))
    if request.method == "POST" and request.POST:
        #获取前端表单数据
        postData = request.POST
        goods_id = postData.get("goods_id")
        goods_name = postData.get("goods_name")
        goods_price = postData.get("goods_price")  # 原价
        goods_now_price = postData.get("goods_now_price")  # 当前价格
        goods_num = postData.get("goods_count")  # 库存
        goods_description = postData.get("goods_description")  # 描述
        goods_content = postData.get("goods_content")  # 详情
        goods_show_time = datetime.datetime.now()  # 发布时间
        goods_adress = postData.get("goods_address")
        types = postData.get("types")  # 一个分类会有多个商品
        #存入数据库
        #先保存商品
        goods = Goods.objects.get(id=int(id))
        goods.goods_id = goods_id
        goods.goods_name = goods_name
        goods.goods_price = goods_price
        goods.goods_now_price = goods_now_price
        goods.goods_num = goods_num
        goods.goods_description = goods_description
        goods.goods_content = goods_content
        goods.goods_show_time = goods_show_time
        #Goods.objects.create(goods_id = goods_id) #增加,参数写在括号里,不需要save
        #Goods.objects.update(goods_id = goods_id) #修改,参数写在括号里,不需要save
        goods.types = Types.objects.get(id=int(types))
        id = request.COOKIES.get("id")
        if id:
            goods.seller = Seller.objects.get(id=int(id))
        else:
            return HttpResponseRedirect("/seller/login/")
        goods.save()

        imgs = request.FILES.getlist("goods_img")
        #保存图片
        for index, img in enumerate(imgs):
            #保存图片到服务器
            file_name = img.name
            file_path = "seller/images/%s_%s.%s" % (
                goods_name, index, file_name.rsplit(".", 1)[1])
            save_path = os.path.join(MEDIA_ROOT, file_path).replace("/", "\\")
            try:
                with open(save_path, "wb") as f:
                    for chunk in img.chunks(chunk_size=1024):
                        f.write(chunk)
                #保存路径到数据库
                i = Image()
                i.img_adress = file_path
                i.img_label = "%s_%s" % (index, goods_name)
                i.img_description = "this is description"
                i.goods = goods
                i.save()
            except Exception as e:
                print(e)
        return HttpResponseRedirect("/seller/goods_list")
    return render(request, "seller/goods_add.html", locals())
Esempio n. 2
0
def goods_add(request):
    postData = request.POST  # post数据
    if request.method == "POST" and request.POST:
        goods_id = postData.get("goods_id")  #商品编号
        goods_name = postData.get("goods_name")  #商品名称
        goods_price = postData.get("goods_price")  #商品原价
        goods_now_price = postData.get("goods_now_price")  #商品现价
        goods_num = postData.get("goods_num")  #商品库存
        goods_description = postData.get("goods_description")  #商品介绍
        goods_content = postData.get("goods_content")  #商品详情
        goods_show_time = datetime.datetime.now()  # 发布时间
        types = postData.get("goods_type")  #商品类型

        # 存入数据库
        t = Goods()
        t.goods_id = goods_id
        t.goods_name = goods_name
        t.goods_price = goods_price
        t.goods_now_price = goods_now_price
        t.goods_num = goods_num
        t.goods_description = goods_description
        t.goods_content = goods_content
        t.goods_show_time = goods_show_time
        t.types = Types.objects.get(id=int(types))
        t.goods_content = goods_content
        id = request.COOKIES.get("id")  #登录时创建的cookis
        if id:  #防非浏览器访问的爬虫
            t.seller = Seller.objects.get(id=int(id))
        else:
            return HttpResponseRedirect("/seller/login/")
        t.save()
        imgs = request.FILES.getlist("userfiles")
        # 保存图片
        for index, img in enumerate(imgs):  #枚举
            # 保存图片到服务器
            file_name = img.name
            file_path = "seller/images/%s_%s.%s" % (
                goods_name, index, file_name.rsplit(".", 1)[1])
            save_path = os.path.join(MEDIA_ROOT, file_path).replace("/", "\\")
            try:
                with open(save_path, "wb") as f:
                    for chunk in img.chunks(chunk_size=1024):
                        f.write(chunk)
                # 保存路径到数据库
                i = Image()
                i.img_adress = file_path
                i.img_label = "%s_%s" % (index, goods_name)
                i.img_description = "this is description"
                i.goods = t
                i.save()
            except Exception as e:
                print(e)
    return render(request, "seller/goods_add.html", locals())