一个商城网站有的购物车,肯定还要一个订单页呀!俗话说装13要装完整的,而代码也给各位补上订单页。
1.视图界面:
#订单界面
def enter_order(request):
if request.method == 'POST':
# 获取地址信息,传递到前端
address_id = request.POST.get('address')
# print('address_id,’---->>>', address_id)
address_obj = Address.objects.get(id=int(address_id))
# 获取选中商品的信息,传递到前端
goods_lst = [] #设一个列表 方便HTML使用for循环显示信息
all_price = 0 # 设一个变量获取所有商品的总价钱
for key, value in request.POST.items(): # value 对应的是id
if key.startswith('name'):
buy_car = BuyCar.objects.get(id=int(value))
price = float(buy_car.goods_price) * int(buy_car.goods_num) # 每类商品的总价
goods_lst.append(
{'price': price, 'buy_car': buy_car}
)
all_price += price
# 生成订单
order = Order()#与数据库建立连接
now = datetime.datetime.now() #时间模块 现在时间
order.order_num = now.strftime("%Y-%m-%d") + str(random.randint(10000, 99999)) #导入事件模块和随机模块生成订单号
order.order_time = now.strftime("%Y-%m-%d")
order.order_statue = 1 # 状态 未支付 1 ,支付成功2 ... #支付状态
order.total = all_price
order.user = Buyer.objects.get(id=int(request.COOKIES.get('user_id')))
order.order_address = address_obj
order.save()#保存
for goods in goods_lst:
buy_car = goods['buy_car']
o_g = OrderGoods()
o_g.goods_id = buy_car.goods_id
o_g.goods_name = buy_car.goods_name
o_g.goods_price = buy_car.goods_price
o_g.goods_num = buy_car.goods_num
o_g.goods_picture = buy_car.goods_picture
o_g.order = order
o_g.save()
return render(request, 'buyer/enterOrder.html', locals())
2.前端代码:
<div class="xq">
<div class="location"><a href="#">我的订单</a> ><a href="">查看订单</a></div>
<div class="shouhuo">
<div class="shouhuo_top">收货信息</div>
<div class="shouhuo_bottom">
<p>收货人:{{ address_obj.recver }}</p>
<p>收货人电话:{{ address_obj.phone }}</p>
<p>收货人地址:{{ address_obj.address }}</p>
<p>支付方式:支付宝</p>
<p>发货时间:2018年9月9日</p>
</div>
</div>
<table width="100%" cellspacing="0" cellpadding="0" border="0" class="table1">
<tr style="height: 50px">
<td width="20%" align="center">商品</td>
<td width="20%" align="center">名称</td>
<td width="20%" align="center">单价</td>
<td width="20%" align="center">数量</td>
<td width="20%" align="center">实付金额</td>
</tr>
</table>
<table width="100%" cellspacing="0" cellpadding="0" border="0" class="table3" style="border-top:none">
{% for i in goods_lst %}
<tr style="height: 50px;border-top:none">
<td width="20%" align="center">
<a href="" target="_blank">
<img src="{{ i.buy_car.goods_picture }}"/>
</a>
</td>
<td width="20%" align="center">
<a href="" target="_blank">{{ i.buy_car.goods_name }}</a>
</td>
<td width="20%" align="center"><span class="price123">{{ i.buy_car.goods_price }}¥</span></td>
<td width="20%" align="center">{{ i.buy_car.goods_num }}</td>
<td width="20%" align="center"><span class="price123">{{ i.price }}</span></td>
</tr>
{% endfor %}
</table>
<table width="100%" cellspacing="0" cellpadding="0" border="0" class="table2">
<tr style="height: 50px" class="table2">
<td width="15%">日期:{{ order.order_time }}</td>
<td width="15%">订单号:{{ order.order_num }}</td>
<td width="70%"></td>
</tr>
</table>
<p style=" text-align: right; line-height: 50px; font-family:'微软雅黑'; font-size: 20px; ">商品总计:¥{{ all_price }}元
<a href="/paymethod/">
<button class="btn" style="margin-left: 40px;">确认下单</button>
</a></p>
</div>
好啦!在这里祝大家把妹顺利装13成功还不被打!