def goods_add(): """ 添加商品 """ form = GoodsForm() # 实例化form表单 supercat_list = [(v.id, v.cat_name) for v in SuperCat.query.all()] # 为super_cat_id添加属性 form.supercat_id.choices = supercat_list # 为super_cat_id添加属性 form.subcat_id.choices = [(v.id, v.cat_name) for v in SubCat.query.filter_by( super_cat_id=supercat_list[0][0]).all() ] # 为super_cat_id添加属性 form.current_price.data = form.data['original_price'] # 为current_pirce 赋值 if form.validate_on_submit(): # 添加商品情况 data = form.data goods = Goods( name=data["name"], supercat_id=int(data['supercat_id']), subcat_id=int(data['subcat_id']), picture=data["picture"], original_price=Decimal(data["original_price"]).quantize( Decimal('0.00')), # 转化为包含2位小数的形式 current_price=Decimal(data["original_price"]).quantize( Decimal('0.00')), # 转化为包含2位小数的形式 is_new=int(data["is_new"]), is_sale=int(data["is_sale"]), introduction=data["introduction"], ) db.session.add(goods) # 添加数据 db.session.commit() # 提交数据 return redirect(url_for('admin.index')) # 页面跳转 return render_template("admin/goods_add.html", form=form) # 渲染模板
def goods_edit(id=None): """ 编辑商品 """ goods = Goods.query.get_or_404(id) form = GoodsForm() # 实例化form表单 form.supercat_id.choices = [(v.id, v.cat_name) for v in SuperCat.query.all()] # 为super_cat_id添加属性 form.subcat_id.choices = [(v.id, v.cat_name) for v in SubCat.query.filter_by(super_cat_id=goods.supercat_id).all()] # 为super_cat_id添加属性 if request.method == "GET": form.name.data = goods.name form.picture.data = goods.picture form.current_price.data = goods.current_price form.original_price.data = goods.original_price form.supercat_id.data = goods.supercat_id form.subcat_id.data = goods.subcat_id form.is_new.data = goods.is_new form.is_sale.data = goods.is_sale form.introduction.data = goods.introduction elif form.validate_on_submit(): goods.name = form.data["name"] goods.supercat_id = int(form.data['supercat_id']) goods.subcat_id = int(form.data['subcat_id']) goods.picture= form.data["picture"] goods.original_price = Decimal(form.data["original_price"]).quantize(Decimal('0.00')) goods.current_price = Decimal(form.data["current_price"]).quantize(Decimal('0.00')) goods.is_new = int(form.data["is_new"]) goods.is_sale = int(form.data["is_sale"]) goods.introduction=form.data["introduction"] db.session.add(goods) # 添加数据 db.session.commit() # 提交数据 return redirect(url_for('admin.index')) # 页面跳转 return render_template("admin/goods_edit.html", form=form) # 渲染模板
def goods_edit(id: int): goods = Goods.query.get_or_404(id) form = GoodsForm() form.supercat_id.choices = [(v.id, v.cat_name) for v in SuperCat.query.all()] form.subcat_id.choices = [ (v.id, v.cat_name) for v in SubCat.query.filter_by(super_cat_id=goods.supercat_id).all() ] if request.method == 'GET': form.name.data = goods.name form.supercat_id.data = goods.supercat_id form.subcat_id.data = goods.subcat_id form.original_price.data = goods.original_price form.picture.data = goods.picture form.is_new.data = goods.is_new form.is_sale.data = goods.is_sale form.introduction.data = goods.introduction elif form.validate_on_submit(): goods.name = form.name.data goods.supercat_id = form.supercat_id.data goods.subcat_id = form.subcat_id.data goods.original_price = form.original_price.data goods.picture = form.picture.data goods.is_new = form.is_new.data goods.is_sale = form.is_sale.data goods.introduction = form.introduction.data db.session.add(goods) db.session.commit() return redirect(url_for('admin.index')) return render_template('admin/goods_edit.html', goods_id=id, form=form)
def goods_edit(): '''编辑商品''' id = request.args.get('id', type=int) page = request.args.get('page', type=int) # print('page',page) # print(request.args) # print('id',id) goods = Goods.query.get_or_404(id) form = GoodsForm() #实例化form表单 form.supercat_id.choices = [ (sup.id, sup.cat_name) for sup in SuperCat.query.all() ] # 为super_cat_id 添加属性 form.subcat_id.choices = [(sub.id, sub.cat_name) for sub in SubCat.query.filter_by( super_cat_id=goods.supercat_id).all()] if request.method == 'GET': form.name.data = goods.name form.picture.data = goods.picture form.current_price.data = goods.current_price form.original_price.data = goods.original_price form.supercat_id.data = goods.supercat_id form.subcat_id.data = goods.subcat_id form.is_new.data = goods.is_new form.is_sale.data = goods.is_sale form.introduction.data = goods.introduction elif form.validate_on_submit(): ran_str = ''.join( random.sample(string.ascii_letters + string.digits, 16)) pic_file = request.files['pic_file'] basedir = '/Users/mac/Desktop/MyPyPro/Pro/shopping_mall_pro/app' path = basedir + "/static/images/goods/" # 图片名称 给图片重命名 为了图片名称的唯一性 pic_file_name = ran_str + '.' + (pic_file.filename).split('.')[-1] # 图片path和名称组成图片的保存路径 file_path = path + pic_file_name # 保存图片 pic_file.save(file_path) # 删除原来图片 ahVRnM8c4sAz9fXF old_file_path = path + goods.picture os.remove(old_file_path) goods.name = form.data['name'] goods.supercat_id = int(form.data['supercat_id']) goods.subcat_id = int(form.data['subcat_id']) goods.picture = pic_file_name goods.original_price = Decimal(form.data['original_price']).quantize( Decimal('0.00')) goods.current_price = Decimal(form.data['current_price']).quantize( Decimal('0.00')) goods.is_new = int(form.data['is_new']) goods.is_sale = int(form.data['is_sale']) goods.introduction = form.data['introduction'] db.session.add(goods) db.session.commit() return redirect(url_for( 'admin.index', page=page)) # 跳转页面 如果想跳转回原来页面,那么请求时候需要带着page 信息 然后此处回复携带 page 信息 return render_template('admin/goods_edit.html', form=form)
def goods_add(): '''添加商品''' form = GoodsForm() # 实例化form表单 supercat_list = [(sup.id, sup.cat_name) for sup in SuperCat.query.all()] # 为大分类super_cat_id添加属性 form.supercat_id.choices = supercat_list # 为大分类标签添加选项 # print(supercat_list) form.subcat_id.choices = [(sub.id, sub.cat_name) for sub in SubCat.query.filter_by( super_cat_id=supercat_list[0][0]).all() ] # 先默认选00 在通过前端get请求区分选了哪个小分类 form.current_price.data = form.data[ 'original_price'] # 为current_price 赋值 默认让现价等于原价 if form.validate_on_submit(): # 验证通过 添加商品情况 data = form.data # 实现图片上传并储存 入库 # 生成随机字符串,防止图片名字重复 ran_str = ''.join( random.sample(string.ascii_letters + string.digits, 16)) pic_file = request.files['pic_file'] basedir = '/Users/mac/Desktop/MyPyPro/Pro/shopping_mall_pro/app' path = basedir + "/static/images/goods/" # 图片名称 给图片重命名 为了图片名称的唯一性 pic_file_name = ran_str + '.' + (pic_file.filename).split('.')[-1] #图片path和名称组成图片的保存路径 file_path = path + pic_file_name # 保存图片 pic_file.save(file_path) goods = Goods( name=data['name'], supercat_id=int(data['supercat_id']), subcat_id=int(data['subcat_id']), # 替换成新命名的图片名称 picture=pic_file_name, original_price=Decimal(data['original_price']).quantize( Decimal('0.00')), # 转化为包含2位小数的形式 current_price=Decimal(data['current_price']).quantize( Decimal('0.00')), is_new=int(data['is_new']), is_sale=int(data['is_sale']), introduction=data['introduction']) db.session.add(goods) db.session.commit() return redirect(url_for('admin.index')) return render_template('admin/goods_add.html', form=form) # get请求渲染模板
def goods_add(): form = GoodsForm() supercat_list = [(item.id, item.cat_name) for item in SuperCat.query.all()] form.supercat_id.choices = supercat_list subcat_list = [(item.id, item.cat_name) for item in SubCat.query.all()] form.subcat_id.choices = subcat_list if form.validate_on_submit(): goods = Goods( name=form.name.data, original_price=Decimal(form.original_price.data).quantize( Decimal('0.00')), current_price=Decimal(form.original_price.data).quantize( Decimal('0.00')), picture=form.picture.data, introduction=form.introduction.data, is_sale=int(form.is_sale.data), is_new=int(form.is_new.data), supercat_id=int(form.supercat_id.data), subcat_id=int(form.subcat_id.data), ) db.session.add(goods) db.session.commit() return redirect(url_for('admin.index')) return render_template('admin/goods_add.html', form=form)