提交订单
def place_order(request):
if request.method == 'GET':
# 从数据库中取数据
user_id = request.session.get('user_id')
shop_carts = ShoppingCart.objects.filter(user_id=user_id, is_select=1)
# 给购物车中的对象绑定一个新增的属性,其值为小计价格
all_total = 0
for carts in shop_carts:
price = carts.nums * carts.goods.shop_price
carts.total = price
all_total += price
# 结算商品个数,总价
carts_count = len(shop_carts)
return render(request, 'place_order.html', {'shop_carts': shop_carts,
'all_total': all_total,
'carts_count': carts_count})
生成订单
def make_order(request):
if request.method == 'POST':
# 创建订单
# 创建订单详情
# 购物车中删除已下单的商品
user_id = request.session['user_id']
# 取购物车中勾选的商品
shop_carts = ShoppingCart.objects.filter(user_id=user_id, is_select=1)
# 计算下单的总价
order_mount = 0
for carts in shop_carts:
order_mount += carts.nums * carts.goods.shop_price
# 生成订单交易号
order_sn = get_order_sn()
# 创建订单
address_id = request.POST.get('address_id')
user_address = UserAddress.objects.filter(pk=address_id).first()
order = OrderInfo.objects.create(user_id=user_id,
order_sn=order_sn,
order_mount=order_mount,
address=user_address.address,
signer_name=user_address.signer_name,
signer_mobile=user_address.signer_mobile)
# 创建订单详情
for carts in shop_carts:
OrderGoods.objects.create(order=order,
goods=carts.goods,
goods_nums=carts.nums)
# 删除购物车中的商品
shop_carts.delete()
request.session.pop('goods')
return JsonResponse({'code': 200, 'msg': '请求成功'})
订单页面
{% extends 'base_main.html' %}
{% block title %}
天天生鲜-提交订单
{% endblock %}
{% block pagename %}
提交订单
{% endblock %}
{% block content %}
<h3 class="common_title">确认收货地址</h3>
<div class="common_list_con clearfix">
<dl>
<dt>寄送到:</dt>
{% for user_address in user.useraddress_set.all %}
<dd><input type="radio" name="address" value="{{ user_address.id }}">{{ user_address.address }} ({{ user_address.signer_name }} 收) {{ user_address.signer_mobile }}</dd>
{% endfor %}
</dl>
<a href="{% url 'user:user_site' %}" class="edit_site">编辑收货地址</a>
</div>
<h3 class="common_title">支付方式</h3>
<div class="common_list_con clearfix">
<div class="pay_style_con clearfix">
<input type="radio" name="pay_style" checked>
<label class="cash">货到付款</label>
<input type="radio" name="pay_style">
<label class="weixin">微信支付</label>
<input type="radio" name="pay_style">
<label class="zhifubao"></label>
<input type="radio" name="pay_style">
<label class="bank">银行卡支付</label>
</div>
</div>
<h3 class="common_title">商品列表</h3>
<div class="common_list_con clearfix">
<ul class="goods_list_th clearfix">
<li class="col01">商品名称</li>
<li class="col02">商品单位</li>
<li class="col03">商品价格</li>
<li class="col04">数量</li>
<li class="col05">小计</li>
</ul>
{% for shop_cart in shop_carts %}
<ul class="goods_list_td clearfix">
<li class="col01">{{ forloop.counter }}</li>
<li class="col02"><img src="/media/{{ shop_cart.goods.goods_front_image }}"></li>
<li class="col03">{{ shop_cart.goods.name }}</li>
<li class="col04">500g</li>
<li class="col05">{{ shop_cart.goods.shop_price }}元</li>
<li class="col06">{{ shop_cart.nums }}</li>
<li class="col07">{{ shop_cart.total }}元</li>
</ul>
{% endfor %}
</div>
<h3 class="common_title">总金额结算</h3>
<div class="common_list_con clearfix">
<div class="settle_con">
<div class="total_goods_count">共<em>{{ carts_count }}</em>件商品,总金额<b>{{ all_total }}元</b></div>
<div class="transit">运费:<b>10元</b></div>
<div class="total_pay">实付款:<b>{{ all_total | add:10 }}元</b></div>
</div>
</div>
{% csrf_token %}
<div class="order_submit clearfix">
<a href="javascript:;" id="order_btn">提交订单</a>
</div>
<div class="popup_con">
<div class="popup">
<p>订单提交成功!</p>
</div>
<div class="mask"></div>
</div>
{% endblock %}
{% block js2 %}
<script type="text/javascript" src="/static/js/jquery-1.12.4.min.js"></script>
<script type="text/javascript">
$('#order_btn').click(function() {
localStorage.setItem('order_finish',2);
var address_radio = $('input[name="address"]');
for(var i=0; i<address_radio.length; i++){
if(address_radio[i].checked == true){
var value = address_radio[i].value;
}
}
if(value){
var csrf = $('input[name="csrfmiddlewaretoken"]').val();
$.ajax({
url: '/order/make_order/',
type: 'POST',
dataType: 'JSON',
data: {
'address_id': value
},
headers: {'X-CSRFToken': csrf},
success: function (data) {
$('.popup_con p').text('订单提交成功');
},
error: function (data) {
$('.popup_con p').text('订单提交失败');
}
});
}else{
$('.popup_con p').text('请选择收货地址!');
}
$('.popup_con').fadeIn('fast', function() {
setTimeout(function(){
$('.popup_con').fadeOut('fast',function(){
window.location.href = 'index.html';
});
},3000)
});
});
</script>
{% endblock %}
网友评论