Пример #1
0
def goods_detail(request, id):
    if request.method == 'GET':
        categorys = GoodsCategory.CATEGORY_TYPE
        if id == '0':
            return render(request, 'goods_detail.html',
                          {'categorys': categorys})
        else:
            good = Goods.objects.get(pk=id)
            return render(request, 'goods_detail.html', {
                'good': good,
                'categorys': categorys
            })
    if request.method == 'POST':
        form = GoodsForm(request.POST, request.FILES)
        if form.is_valid():
            if id == '0':
                data = form.cleaned_data
                Goods.objects.create(**data)
                return HttpResponseRedirect(reverse('goods:goods_list'))
            else:
                data = form.cleaned_data
                goods_front_image = data.pop('goods_front_image')
                if goods_front_image:
                    goods = Goods.objects.filter(pk=id).first()
                    goods.goods_front_image = goods_front_image
                    goods.save()
                Goods.objects.filter(pk=id).update(**data)
                return HttpResponseRedirect(reverse('goods:goods_list'))
        else:
            return render(request, 'goods_detail.html')
Пример #2
0
def goods_edit(request, id):
    if request.method == 'GET':
        # 编辑商品对象
        goods = Goods.objects.filter(pk=id).first()
        types = GoodsCategory.CATEGORY_TYPE
        return render(request, 'goods_detail.html', {'goods': goods, 'types': types})

    if request.method == 'POST':
        # 1. form表单校验
        form = GoodsForm(request.POST, request.FILES)
        if form.is_valid():
            # 验证成功
            data = form.cleaned_data
            # 把图片从data中删掉,
            # img表示更新商品时,选择了图片,则img为图片内容
            # 如果更新商品时,没有选择图片,则img为None
            img = data.pop('goods_front_image')
            # 更新除了图片的其他字段信息
            Goods.objects.filter(pk=id).update(**data)
            if img:
                # 更新图片的信息
                goods = Goods.objects.filter(pk=id).first()
                goods.goods_front_image = img
                goods.save()
            return HttpResponseRedirect(reverse('goods:goods_list'))
        else:
            # 修改验证失败
            goods = Goods.objects.filter(pk=id).first()
            types = GoodsCategory.CATEGORY_TYPE
            return render(request, 'goods_detail.html', {'errors':form.errors, 'goods': goods, 'types': types})
Пример #3
0
def goods_update(request, id):
    if request.method == 'GET':
        # 编辑商品对象
        types = GoodsCategory.CATEGORY_TYPE
        goods = Goods.objects.filter(pk=id).first()
        return render(request, 'goods_update.html', {
            'goods': goods,
            'types': types
        })
    if request.method == 'POST':
        form = GoodsForm(request.POST, request.FILES)
        if form.is_valid():
            data = form.cleaned_data
            # 把图片从data中删掉
            # img 表示更新商品时,选择了图片,则img为图片内容
            # 如果更新的时候,没有选择图片,则img为None
            img = data.pop('goods_front_image')
            Goods.objects.filter(pk=id).update(**data)
            if img:
                goods = Goods.objects.filter(pk=id).first()
                goods.goods_front_image = img
                goods.save()
            return HttpResponseRedirect(reverse('goods:goods_list'))
        else:
            goods = Goods.objects.filter(pk=id).first()
            types = GoodsCategory.CATEGORY_TYPE
            errors = form.errors
            return render(request, 'goods_update.html', {
                'errors': errors,
                'goods': goods,
                "types": types
            })
Пример #4
0
def goods_edit(request, id):
    if request.method == 'GET':
        # 获取需要编辑的商品对象
        goods = Goods.objects.get(pk=id)
        category_types = GoodsCategory.CATEGORY_TYPE
        return render(request, 'goods_detail.html', {'goods': goods, 'category_types': category_types})
    if request.method == 'POST':
        form = GoodsForm(request.POST, request.FILES)
        if form.is_valid():
            # 获取表单验证中的参数,其中包含了封面图的键值对
            data = form.cleaned_data
            # 从字典中剔出封面图
            goods_front_image = data.pop('goods_front_image')
            # 判断,如果修改了封面图,则进行单独的修改
            if goods_front_image:
                goods = Goods.objects.filter(pk=id).first()
                goods.goods_front_image = goods_front_image
                goods.save()
            # 更新操作
            Goods.objects.filter(pk=id).update(**data)
            return HttpResponseRedirect(reverse('goods:goods_list'))
        else:
            # 验证表单失败
            goods = Goods.objects.get(pk=id)
            category_types = GoodsCategory.CATEGORY_TYPE
            return render(request, 'goods_detail.html',
                          {'goods': goods, 'category_types': category_types,
                           'form': form})
Пример #5
0
def goods_edit(request,id):
    if request.method == 'GET':
        # 要编辑的商品对象
        goods = Goods.objects.filter(pk=id).first()
        types = GoodsCategory.CATEGORY_TYPE
        return render(request,'back/goods_detail.html',{'goods':goods,'types':types})
    if request.method == 'POST':
        form = GoodsForm(request.POST, request.FILES)
        if form.is_valid():
            data = form.cleaned_data
            # 把图片从data中剔除
            # 更新时没有选择图片,图片字段为None,有图片字段为图片名字,没有路径前缀
            img = data.pop('goods_front_image')
            if img:
                goods = Goods.objects.filter(pk=id).first()
                goods.goods_front_image = img
                goods.save()
            # 更新除了图片的其他信息
            Goods.objects.filter(pk=id).update(**data)
            return HttpResponseRedirect(reverse('goods:goods_list'))
        else:
            # 验证失败
            goods = Goods.objects.filter(pk=id).first()
            types = GoodsCategory.CATEGORY_TYPE
            return render(request, 'back/goods_detail.html', {'errors': form.errors,'types':types,'goods':goods})
Пример #6
0
def goods_detail(request):
    if request.method == 'GET':
        categorys = GoodsCategory.CATEGORY_TYPE
        return render(request, 'goods_detail.html', {'categorys': categorys})

    if request.method == 'POST':
        form = GoodsForm(request.POST, request.FILES)
        if form.is_valid():
            """第一种创建方法:
            Goods.objects.create(name=form.cleaned_data.get('name'),
                                 goods_sn=form.cleaned_data.get('goods_sn'),
                                 category=GoodsCategory.objects.filter(pk=form.cleaned_data.get('category')).first(),
                                 # category_id
                                 goods_nums=form.cleaned_data.get('goods_nums'),
                                 market_price=form.cleaned_data.get('market_price'),
                                 shop_price=form.cleaned_data.get('shop_price'),
                                 goods_brief=form.cleaned_data.get('goods_brief'),
                                 goods_front_image=request.FILES.get('goods_front_image'))
            """
            # 第二种创建方法
            data = form.cleaned_data
            # ca
            Goods.objects.create(**data)
            return HttpResponseRedirect(reverse('goods:goods_list'))
        else:
            return render(request, 'goods_detail.html',
                          {'errors': form.errors})
Пример #7
0
def goods_detail(request):
    if request.method == 'GET':
        categorys = GoodsCategory.CATEGORY_TYPE
        return render(request, 'goods_detail.html', {'categorys': categorys})
    if request.method == 'POST':
        # 验证商品的信息的完整性 数据的保存
        form = GoodsForm(request.POST)
        if form.is_valid():
            # 创建
            data = form.cleaned_data
            Goods.objects.create(**data)
            return HttpResponseRedirect(reverse('goods:goods_list'))
        else:
            return render(request, 'goods_detail.html', {'errors': form.error})
Пример #8
0
def goods_add(request):
    if request.method == 'GET':
        types = GoodsCategory.CATEGORY_TYPE
        return render(request,'back/goods_detail.html',{'types':types})
    if request.method == 'POST':
        # TODO:验证商品信息的完整性,数据的保存
        # 使用表单验证
        form = GoodsForm(request.POST,request.FILES)
        if form.is_valid():
            data = form.cleaned_data
            Goods.objects.create(**data)
            return HttpResponseRedirect(reverse('goods:goods_list'))
        else:
            # 验证失败
            return render(request,'back/goods_detail.html',{'errors':form.errors})
Пример #9
0
def goods_detail(request):
    if request.method == 'GET':
        # 获取所有的分类
        types = GoodsCategory.CATEGORY_TYPE
        return render(request, 'goods_detail.html', {'types': types})
    if request.method == 'POST':
        # 使用表单验证
        form = GoodsForm(request.POST, request.FILES)
        if form.is_valid():
            # 创建 获取form所有信息
            data = form.cleaned_data
            Goods.objects.create(**data)
            return HttpResponseRedirect(reverse('goods:goods_list'))
        else:
            # 验证失败
            return render(request, 'goods_detail.html')
Пример #10
0
def goods_add(request):
    if request.method == 'GET':
        categorys = GoodsCategory.CATEGORY_TYPE
        return render(request, 'goods_detail.html', {'categorys': categorys})

    if request.method == 'POST':
        # 1. 使用表单验证
        form = GoodsForm(request.POST, request.FILES)
        if form.is_valid():
            # 创建
            data = form.cleaned_data
            # category=form.cleaned_data.get('category')
            # category=对象
            Goods.objects.create(**data)
            return HttpResponseRedirect(reverse('goods:goods_list'))
        else:
            # 验证失败
            return render(request, 'goods_detail.html', {'errors': form.errors})
Пример #11
0
def goods_add(request):
    if request.method == 'GET':
        # TODO:页面中刷新分页信息
        categorys = GoodsCategory.CATEGORY_TYPE
        return render(request, 'goods_detail.html', {'categorys': categorys})

    if request.method == 'POST':
        # TODO: 验证商品信息的完整性,数据的保存
        form = GoodsForm(request.POST, request.FILES)
        if form.is_valid():
            data = form.cleaned_data
            # category = form.cleaned_date.get('category')
            Goods.objects.create(**data)
            return HttpResponseRedirect(reverse('goods:goods_list'))
        else:
            # 验证失败
            errors = form.errors
            return render(request, 'goods_detail.html', {'errors': errors})
Пример #12
0
def goods_detail(request):
    if request.method == 'GET':
        category_types = GoodsCategory.CATEGORY_TYPE
        return render(request, 'goods_detail.html',
                      {'category_types': category_types})
    if request.method == 'POST':
        form = GoodsForm(request.POST, request.FILES)
        if form.is_valid():
            # *args(把参数传入一个元组) **kwargs(字典)
            data = form.cleaned_data
            # goods_front_image = request.FILES.get('goods_front_image')
            # category = request.POST.get('category')
            # 保存
            Goods.objects.create(**data)
            return HttpResponseRedirect(reverse('goods:goods_list'))
        else:
            # 字段验证不通过,返回页面,并在页面使用form.errors拿到错误信息
            return render(request, 'goods_detail.html', {'form': form})
Пример #13
0
def goods_add(request):
    if request.method == 'GET':
        category_types = GoodsCategory.CATEGORY_TYPE
        return render(request, 'goods_detail.html',
                      {'category_types': category_types})

    if request.method == 'POST':
        # 保存商品数据,并跳转到商品列表页面
        # 1.获取页面中传递的参数,并用form表单验证
        # 2.保存
        # 3.跳转到列表页面
        form = GoodsForm(request.POST, request.FILES)
        if form.is_valid():
            # 保存, *args  **kwargs
            data = form.cleaned_data
            Goods.objects.create(**data)
            # 跳转到列表页面
            return HttpResponseRedirect(reverse('goods:goods_list'))
        else:
            return render(request, 'goods_detail.html', {'form': form})
Пример #14
0
def goods_add(request):
    if request.method == 'GET':
        category_types = GoodsCategory.CATEGORY_TYPE
        return render(request, 'goods_detail.html', {'category_types': category_types})

    if request.method == 'POST':
        # 保存商品数据,并跳转到商品列表页面
        # 1. 获取页面中传递的参数,并校验是否填写完整
        # 2. 保存
        # 3. 跳转到列表页面
        form = GoodsForm(request.POST, request.FILES)
        if form.is_valid():
            # 保存, def xxx(*args, **kwargs)
            data = form.cleaned_data
            # 保存
            Goods.objects.create(**data)
            # 跳转到列表页面
            return HttpResponseRedirect(reverse('goods:goods_list'))
        else:
            # 字段验证不通过,返回页面,并在页面中使用form.errors拿到错误信息
            return render(request, 'goods_detail.html', {'form': form})
Пример #15
0
def goods_add(request):
    if request.method == 'GET':
        # TODO:页面中刷新分类信息
        categorys = GoodsCategory.CATEGORY_TYPE
        return render(request, 'goods_detail.html', {'categorys': categorys})
    if request.method == 'POST':
        # TODO:验证商品信息的完整性,数据的保存
        # category = GoodsCategory.objects.filter(category_type=request.POST.get('category')).first()
        #
        # goods = Goods()
        # goods.name = request.POST.get('name')
        # goods.goods_sn=request.POST.get('goods_sn')
        # goods.category=category
        # goods.goods_nums=request.POST.get('goods_nums')
        # goods.market_price=request.POST.get('market_price')
        # goods.shop_price=request.POST.get('shop_price')
        # goods.goods_front_image=request.FILES.get('goods_front_image')
        # goods.save()

        #1.使用表单验证
        form = GoodsForm(request.POST, request.FILES)
        if form.is_valid():
            # 创建
            # Goods.objects.create(name=form.cleaned_data.get('name'),
            #                      category_id=form.cleaned_data.get('category'),
            #                      或者category=对象)
            # **data字典
            data = form.cleaned_data
            #category(对象)=form.cleaned_data.get('category')(id值=1)
            #(1)category_id
            #(2)category在form中重写方法,变成对象
            Goods.objects.create(**data)
            return HttpResponseRedirect(reverse('goods:goods_list'))
        else:
            #验证失败
            return render(request, 'goods_detail.html',
                          {'errors': form.errors})