添加商品放入購物車
添加商品放入購物車:
templates:
將該商品id利用POST表單傳進後台
detail.html
...
<form action="{% url 'cart_add' goods.id %}" method="POST">
...
urls.py
'^cart/add/(?P<gid>[0-9]+)$', cart.add, name="cart_add"),
cart.py
def add(request,gid):
# 建立shoplist
goods = Goods.objects.get(id=gid)
shop = goods.toDict()
# 獲取購買商品數量
shop['m'] = int(request.POST.get("m",1))
shoplist = request.session.get("shoplist",{})
# 判断购物车中是否已存在要购买的商品
if gid in shoplist:
shoplist[gid]['m'] += shop['m'] #累加购买量
else:
shoplist[gid] = shop #將信息內容加入購物車商品
#将购物车中的商品信息放回到session中
request.session['shoplist'] = shoplist
#跳转查看购物车
return redirect(reverse('cart_index'))
urls.py
url(r'^cart$', cart.index, name="cart_index"),
cart.py
def index(request):
'''浏览购物车'''
context = loadinfo(request)
if 'shoplist' not in request.session:
request.session['shoplist']={}
return render(request,"web/cart.html",context)
cart.html
<table class="cart-merchant-body">
<tbody>
<!-- for in 輸出購物車內商品的值-->
{% for shop in request.session.shoplist.values %}
...<!-- 數量加減時可用JS或AJAX, 搭配模板加減法-->
<button onclick="window.location='{% url 'cart_change' %}?gid={{shop.id}}&num={{shop.m|add:-1}}'" class="mz-adder-subtract"></button>
<div class="mz-adder-num"><input class="mz-adder-input" value="{{ shop.m }}" onblur="window.location='{% url 'cart_change' %}?gid={{shop.id}}&num='+this.value" type="text"></div>
<button onclick="window.location='{% url 'cart_change' %}?gid={{shop.id}}&num={{shop.m|add:1}}'" class="mz-adder-add"></button>
...
{% endfor %}
...
<!-- 结算详情 -->
<div onclick="window.location='/orders/add?ids='+loadTotal().join(',')" class="mz-btn btn-success" id="cartSubmit">去结算</div>
</div>
...
urls.py
url(r'^orders/add$', orders.add,name='orders_add'), #订单的表单页
order.py
def add(request):
'''下订单第一步:订单表单'''
context = loadinfo(request)
# 获取要结算商品的id号
ids = request.GET.get("ids",'')
if len(ids) == 0:
context = {"info":"请选择要结算的商品!"}
return render(request,"web/ordersinfo.html",context)
gidlist = ids.split(',')
# 从购物车获取要结算所有商品,并放入到orderslist中,并且累计总金额
shoplist = request.session['shoplist']
orderslist = {}
total = 0.0
for gid in gidlist:
orderslist[gid] = shoplist[gid]
total += shoplist[gid]['price']*shoplist[gid]['m']
# 将这些信息放入到session中
request.session['orderslist'] = orderslist
request.session['total'] = total
return render(request,"web/ordersadd.html",context)
ordersadd.html
...
<form action="{% url 'orders_confirm' %}" method="post">
<!-- 均使用session作為輸出-->
<input type="text" name="linkman" value="{{ request.session.vipuser.name}}" size="40"/>
<input type="text" name="address" value="{{ request.session.vipuser.address}}" size="40"/>
<input type="text" name="phone" value="{{ request.session.vipuser.phone}}" size="40"/>
<input type="text" name="code" value="{{ request.session.vipuser.code}}" size="40"/>
<input type="text" disabled value="{{ request.session.total }} 元" name="linkman" size="40"/>
...
<!-- 返回JS -->
<div onclick="window.history.go(-1)" class="mz-btn btn-success" id="cartSubmit">返回</div>
<button type="submit" class="mz-btn btn-success" id="cartSubmit">下一步</button>
</form>
urls.py
url(r'^orders/confirm$', orders.confirm,name='orders_confirm'), #订单确认页
orders.py
def confirm(request):
context = loadinfo(request)
return render(request,"web/ordersconfirm.html",context)
ordersconfirm.html
...
<form action="{% url 'orders_insert' %}" method="post">
<input readonly type="text" name="linkman" value="{{ request.POST.linkman}}" size="40"/>
<input readonly type="text" name="address" value="{{ request.POST.address}}" size="40"/>
<input readonly type="text" name="phone" value="{{ request.POST.phone}}" size="40"/>
<input readonly type="text" name="code" value="{{ request.POST.code}}" size="40"/>
<div onclick="window.history.go(-1)" class="mz-btn btn-success" id="cartSubmit">返回修改</div>
<button type="submit" class="mz-btn btn-success" id="cartSubmit">确认下单</button>
</form>
<!-- 订单清单信息列表 -->
{% for shop in request.session.orderslist.values %}
{{ shop.goods }}
<img src="/static/goods/s_{{ shop.picname }}" alt="{{ shop.goods }}" width="50">
{{ shop.price}}
{{ shop.m }}
{% widthratio shop.price 1 shop.m %}
{% endfor %}
urls.py
url(r'^orders/insert$', orders.insert,name='orders_insert'), #执行订单添加操作
orders.py
def insert(request):
context = loadinfo(request)
try:
# 执行订单信息添加操作 Orders()
od = Orders()
od.uid = request.session['vipuser']['id'] #当前登录者的id号切記要從session獲取,重要的資訊別從使用端直接獲取,而是先經過與資料庫端比對之後再從資料庫中獲取值。
od.linkman = request.POST.get('linkman')
od.address = request.POST.get('address')
od.code = request.POST.get('code')
od.phone = request.POST.get('phone')
od.addtime = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
od.total = request.session['total']
od.state = 0
od.save()
# 执行订单详情添加 Detail()
orderslist = request.session['orderslist']
shoplist = request.session['shoplist']
for shop in orderslist.values():
del shoplist[str(shop['id'])]
ov = Detail()
ov.orderid = od.id
ov.goodsid = shop['id']
ov.name = shop['goods']
ov.price = shop['price']
ov.num = shop['m']
ov.save()
del request.session['orderslist']
del request.session['total']
request.session['shoplist'] = shoplist
context = {"info":"订单添加成功!订单号:"+str(od.id)}
return render(request,"web/ordersinfo.html",context)
except Exception as err:
print(err)
context = {"info":"订单添加失败,请稍后再试!"}
return render(request,"web/ordersinfo.html",context)
以上完成購買行為,資料庫及使用端雙方都獲取一個orderid訂單號碼
orderslist
{‘4’: {‘id’: 4, ‘typeid’: 4, ‘goods’: ‘佳能DS6000’, ‘company’: ‘佳能品牌’, ‘price’: 2880.0, ‘picname’: ‘1523416548.696832.jpg’, ‘store’: 30, ‘num’: 0, ‘clicknum’: 13, ‘state’: 1, ‘m’: 8}, ‘10’: {‘id’: 10, ‘typeid’: 4, ‘goods’: ‘佳能数码相机’, ‘company’: ‘佳能品牌’, ‘price’: 800.0, ‘picname’: ‘1523417097.9236016.jpg’, ‘store’: 30, ‘num’: 0, ‘clicknum’: 3, ‘state’: 2, ‘m’: 5}}