跟学妹说,不止会购物车,还会Django订单页!

一个商城网站有的购物车,肯定还要一个订单页呀!俗话说装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成功还不被打!

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值