订单确认页面

进入应用myhome/urls.py,并增加路径

1
2
3
4
from . views import OrderViews
# 订单 确认订单,提交订单,订单支付
url(r'^order/confirm/', OrderViews.myhome_order_confirm, name="myhome_order_confirm"),
url(r'^order/create/', OrderViews.myhome_order_create, name="myhome_order_create"),

进入应用myhome/views,创建OrderViews.py,并增加myhome_order_confirm函数

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
  from django.shortcuts import render
from django.http import HttpResponse,Http404,JsonResponse
from myadmin.models import Cates,Goods,Cart,Users,Order,OrderInfo

# 订单确认页
def myhome_order_confirm(request):
# 获取选择的购物车id
cartidstr = request.GET.get('cartids')
cartids = cartidstr.split(',')
# 把当前选择的购物车数据查询处理
data = Cart.objects.filter(id__in=cartids)
# 分配数据
context = {'cartdata':data}
# 加载模板
return render(request,'myhome/order/confirm.html',context)
# 订单的创建
def myhome_order_create(request):
return JsonResponse('订单的创建')

进入templates/myhome/order/confirm.html,并输入

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
{% extends 'myhome/index.html' %}

{% block title %}
<title>订单确认页</title>
{% endblock %}

{% block css %}
<link rel="stylesheet" type="text/css" href="/static/myhome/public/css/cart.css">
<link rel="stylesheet" type="text/css" href="/static/myhome/public/css/cart-app.css">
{% endblock %}

{% block con %}
<form class="form-inline" action="{% url 'myhome_order_create' %}" method="POST">
{% csrf_token %}
<div class="mainbody cart" style="padding-top: 70px;">
<div class="container">
<div class="row">
<br>
<div class="col-md-12">
<div style="height: 160px;background: #fff;">
<div class="col-md-8">
<h4>填写收货信息</h4>
</div>
<div class="col-md-4">
<h4>选择支付方式</h4>
</div>
<div class="col-md-8">
<div class="form-group">
<label for="exampleInputName2">收货人</label>
<input type="text" name="username" class="form-control" placeholder="收货人">
</div>
<div class="form-group">
<label for="exampleInputEmail2">联系电话</label>
<input type="email" name="phone" class="form-control" placeholder="联系电话">
</div>

<div class="form-group" style="margin-top: 20px;">
<label for="exampleInputEmail2">收货地址</label>
<input type="text" name="address" class="form-control" size="50" placeholder="收货地址">
</div>
<!-- 隐藏域 传递当前的购物车id -->
<input type="hidden" name="cartids" value="{{ request.GET.cartids }}">
</div>
<div class="col-md-2" style="height: 40px;border:1px solid red;margin: 10px;text-align: center;line-height: 40px;">
支付宝
</div>
<div class="col-md-2" style="height: 40px;border:1px solid #ccc;margin: 10px;text-align: center;line-height: 40px;">
货到付款
</div>
</div>
</div>
</div>
<!-- 购物车详情头 -->
<table class="cart-header">
<tbody>
<tr>
<td class="cart-col-select col-md-3 col-xs-3 col-sm-3">
<div class="cart-select-all JSelectAll">
<h4>购物清单</h4>
</div>
</td>
<td class="cart-col-name col-md-3 hidden-xs hidden-sm">商品</td>
<td class="cart-col-price col-md-2 hidden-xs hidden-sm">单价(元)</td>
<td class="cart-col-number col-md-2 hidden-xs hidden-sm">数量</td>
<td class="cart-col-total col-md-1 hidden-xs hidden-sm">小计(元)</td>
</tr>
</tbody>
</table><!-- 购物车详情头 E-->

<!-- 购物清单信息列表 -->
<div class="cart-merchant-list">
<div class="cart-merchant">
<table class="cart-merchant-body">
<tbody>
{% for v in cartdata %}
<tr class="cart-product">
<td class="cart-col-select col-md-3 col-xs-4 col-sm-4">
<a href="meilanx.html" class="cart-product-link" target="_blank">
<img src="{{ v.goodsid.pic_url }}" class="cart-product-img" alt="魅蓝 X">
</a>
</td>
<td class="cart-col-name col-md-3 col-xs-8 col-sm-8">
<a href="meilanx.html" class="cart-product-link" target="_blank">
<p>{{ v.goodsid.goodsname }}</p>
<span class="cart-product-desc">{{ v.goodsid.title }}</span>
</a>

</td>
<td class="cart-col-price col-md-2 hidden-xs hidden-sm">
<p>
<span class="cart-product-price">{{ v.goodsid.price }}</span>
</p>
</td>
<td class="cart-col-number col-md-2 hidden-xs hidden-sm">
<div class="cart-product-number-adder">
<p class="cart-product-number-max show"></p>
<div class="mz-adder" style="text-align: center;line-height: 40px;">
<h5>{{ v.num }}</h5>
</div>
</div>
</td>
<td class="cart-col-total col-md-1 hidden-xs hidden-sm">
{% load pagetag %}
<span class="cart-product-price total">{% cheng v.num v.goodsid.price %}</span>
</td>
</tr>
{% endfor %}
</tbody>
</table>
</div>
</div><!-- 购物清单信息列表 E-->
</div>
<!-- 结算详情 -->
<div class="cart-footer" id="cartFooter">
<div class="container">
<div class="cart-footer-left col-md-6 col-xs-4 col-sm-4">
<!-- <span class="cart-remove-selected" id="removeSelected">删除选中的商品</span> -->
<span class="cart-footer-count">

<span class="cart-footer-num" id="totalCount"></span>
件商品
</span>
</div>
<div class="cart-footer-right col-md-5 col-md-offset-1 col-sm-offset-2 col-xs-8 col-sm-6">
<span class="cart-footer-sum">
<span class="cart-footer-text">已优惠</span>
<span class="cart-footer-num red" id="totalDiscount">0.00</span>
<span class="cart-footer-text">元, 合计(不含运费):</span>
<span class="cart-footer-total" id="totalPrice">0.00</span>
</span>
<div class="mz-btn success" id="cartSubmit">提交订单</div>
</div>
</div>
</div><!-- 结算详情 E-->
</div>
<!-- 主内容区域 E-->
</form>
{% endblock %}

{% block js %}
<script type="text/javascript">
//回顶部
topLogin();
backTop();
var totalPrice = 0
// 订单提交事件
$('#cartSubmit').click(function(){
// 检测是否填写了收货信息
$('form').submit()
})

// 计算总价
$('.total').each(function(){
totalPrice += Number($(this).text())
})
$('#totalPrice').text(returnFloat(totalPrice))

// 计算总数
var totalCount = 0
$('.mz-adder').each(function(){
totalCount+= Number($(this).find('h5').text())
})
$('#totalCount').text(totalCount)

function returnFloat(value){
var value=Math.round(parseFloat(value)*100)/100;
var xsd=value.toString().split(".");
if(xsd.length==1){
value=value.toString()+".00";
return value;
}
if(xsd.length>1){
if(xsd[1].length<2){
value=value.toString()+"0";
}
return value;
}
}
</script>
{% endblock %}

订单的提交

进入应用myadmin/models.py,添加订单模型

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# 订单
class Order(models.Model):
uid = models.ForeignKey(to="Users", to_field="id",on_delete=models.CASCADE)
username = models.CharField(max_length=20)
phone = models.CharField(max_length=11)
address = models.CharField(max_length=255)
totalprice = models.FloatField()
# 0 未支付, 1已支付, 2已发货 3已收货 4已评价 5取消 ...
status = models.IntegerField(default=0)
# 0 支付宝 1 微信 2 银联 3 其它 4货到付款...
paytype = models.IntegerField(default=0)
addtime = models.DateTimeField(auto_now_add=True)
paytime = models.DateTimeField(null=True)

# 订单详情
class OrderInfo(models.Model):
orderid = models.ForeignKey(to="Order", to_field="id",on_delete=models.CASCADE)
goodsid = models.ForeignKey(to="Goods", to_field="id",on_delete=models.CASCADE)
num = models.IntegerField()
'''
# 订单
id订单号 uid用户 收货人,收货地址,收货电话,总价,状态 支付方式 ,下单时间,支付时间
100011 189
# 订单详情
orderid goodsid num price购买时的单价
100011 12 1
100011 18 3
'''

进入pycharm的终端输入迁移语句

进入应用myhome/views/CartViews.py,并修改myhome_order_create函数

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
from django.shortcuts import reverse
# 订单的创建
def myhome_order_create(request):
# 接受数据
data = request.POST.dict()
data.pop('csrfmiddlewaretoken')
# {'username': '王五', 'address': '北京市', 'cartids': '9,10', 'phone': '13701383017'}
# 创建订单
ob = Order()
ob.uid = Users.objects.get(id=request.session.get('VipUser')['uid'])
ob.username = data['username']
ob.phone = data['phone']
ob.address = data['address']
ob.totalprice = 0
ob.save()

# 创建订单详情
cartdata = Cart.objects.filter(id__in = data['cartids'].split(','))
totalprice = 0
for x in cartdata:
# 创建订单详情
obinfo = OrderInfo()
obinfo.orderid = ob
obinfo.goodsid = x.goodsid
obinfo.num = x.num
obinfo.save()
# 计算总价
totalprice += x.num*x.goodsid.price
# 删除购物车中已经下单的商品
x.delete()
# 修改订单的总价
ob.totalprice = totalprice
ob.save()
# 重定向到支付页面
return HttpResponse('<script>alert("订单创建成功,请支付");location.href="'+reverse('myhome_order_pay')+'?orderid='+str(ob.id)+'"</script>')

进入应用myhome/urls.py,并增加路径

1
url(r'^order/pay/', OrderViews.myhome_order_pay, name="myhome_order_pay"),

进入应用myhome/views/CartViews.py,并修改myhome_order_pay函数

1
2
3
4
# 订单支付
def myhome_order_pay(request):
orderid = request.GET.get('orderid')
return HttpResponse('请支付您的订单.订单号为:'+str(orderid))

个人中心-我的订单

进入应用myhome/urls.py,并增加路径

1
2
3
from . views import CenterViews
# 个人中心 我的订单 个人信息 地址管理
url(r'^center/order/',CenterViews.myhome_center_order,name="myhome_center_order"),

进入应用myhome/views,创建CenterViews.py,并增加函数

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
from django.shortcuts import render
from django.http import HttpResponse,Http404,JsonResponse
from myadmin.models import Cates,Goods,Cart,Users,Order,OrderInfo

# 个人中心 视图
# 我的订单
def myhome_center_order(request):
# 获取当前用户的所有订单
orderdata = Order.objects.filter(uid=request.session.get('VipUser')['uid'])
totalprice = 0
for x in orderdata:
orderinfo = x.orderinfo_set.all()
for y in orderinfo:
totalprice += y.goodsid.price*y.num
x.totalprice = totalprice
# 分配数据
context = {'orderdata':orderdata}
# 加载模板
return render(request,'myhome/center/order.html',context)

进入templates/myhome,创建center目录,并在此目录下创建order.html

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
{% extends 'myhome/index.html' %}

{% block title %}
<title>我的订单</title>
{% endblock %}

{% block css %}
<link rel="stylesheet" type="text/css" href="/static/myhome/public/css/order.css">
<link rel="stylesheet" type="text/css" href="/static/myhome/public/css/order-app.css">
{% endblock %}

{% block con %}
<div class="mainbody order">
<div class="container">
<!-- 面包屑导航 -->
<div class="crumbs col-xs-12 col-sm-12">
<ol class="breadcrumb">
<li class="hidden-xs hidden-sm"><a href="index.html">首页</a></li>
<li class="hidden-xs hidden-sm"><a href="member.html">我的商城</a></li>
<li class="active">我的订单</li>
</ol>
</div><!-- 面包屑导航 E-->

<div class="main clearfix">
<!-- 左侧导航 -->
<div class="left-nav f-fl col-md-4 hidden-xs hidden-sm">
<div class="nav-main">
<a href="javascript:;" class="type-title"><span class="glyphicon glyphicon-list-alt" aria-hidden="true"></span>订单中心</a>
<a href="{% url 'myhome_center_order' %}" class="ml active" >我的订单</a>
<a href="#" class="ml " >我的回购单</a>
<a href="#" class="ml " >我的意外保</a>
<a href="javascript:;" class="type-title"><span class="glyphicon glyphicon-user" aria-hidden="true"></span>个人中心</a>
<a href="#" class="ml " >地址管理</a>
<a href="#" class="ml " >我的收藏</a>
<a href="#" class="ml " >消息提醒</a>
<a href="#" class="ml " >建议反馈</a>
</div>
</div><!-- 左侧导航 E-->
<!-- 右侧内容展示 -->
<div class="right-content f-fr col-md-8 col-xs-12 col-sm-12">
<div class="order-main">
<div class="type-tab-btn">
<a href="javascript:;" class="allOrder active col-20" data-type="-1">全部订单</a><i class="line hidden-xs hidden-sm">|</i>
<a class="waitPay col-20" href="javascript:;" data-type="0">待付款<span class="amount _actAmount"></span></a><i class="line hidden-xs hidden-sm">|</i>
<a class="waitDeliver col-20" href="javascript:;" data-type="1">待发货</a><i class="line hidden-xs hidden-sm">|</i>
<a class="hasDeliver col-20" href="javascript:;" data-type="2">已发货</a><i class="line hidden-xs hidden-sm">|</i>
<a class="other col-20" href="javascript:;" data-type="99">其他</a>
</div>
<div class="list-head hidden-xs hidden-sm">
<ul class="clearfix">
<li class="w50">
<select id="checkType" class="check-type">
<option value="0">近三个月的订单</option>
<option value="1">全部订单</option>
</select>
订单明细
</li>
<li class="w125">售后</li>
<li class="w125">金额</li>
<li class="w125">状态</li>
<li class="w125">操作</li>
</ul>
</div>
<div id="tableList" class="type-contain ui-load-container">
<div class="ui-load-content"><input id="unPayNum" value="0" type="hidden">
<table class="orderItem">
<tbody>
{% for v in orderdata %}
<tr class="trHead hidden-xs hidden-sm">
<td colspan="4" class="title clearfix">
<div class="f-fl">
下单时间:<span class="time">{{ v.addtime|date:'Y-m-d H:i:s' }}</span>
订单号:<span class="orderNumber">{{ v.id }}</span>
</div>
</td>
</tr>
<tr class="list-box b-l b-r b-b">
<td class="list b-r j-iamCart">
<div class="cart-wrap j-CartWrap">
<div class="shop j-shop j-amLight">
{% for vv in v.orderinfo_set.all %}
<div class="item b-t clearfix j-item j-iamMain">
<a class="productDetail nameWidth col-xs-4 col-sm-4" href="#" target="_blank">
<img src="{{ vv.goodsid.pic_url }}" class="f-fl">
</a>
<div class="describe f-fl col-xs-8 col-sm-8">
<div class="vertic clearfix">
<span class="clearfix">
<a class="productDetail nameWidth" href="#" target="_blank">
<i>{{ vv.goodsid.title}}</i> </a>
<p>
¥{{ vv.goodsid.price }}×{{ vv.num }}
</p>
</span>
</div>
</div>
<input class="orderSn" value="51090244361541573081" type="hidden">
<input class="isCart" value="1" type="hidden">
<input class="orderSnSon" value="21090244361541575081" type="hidden">
</div>
{% endfor %}

</div>
</div>
</td>
<td class="b-r w125 center price b-t hidden-xs hidden-sm">
<div class="priceDiv">
¥ {{ v.totalprice }}

</div>
</td>
<td class="b-r w125 center state b-t hidden-xs hidden-sm">
<div class="stateDiv">
{% if v.status == 0%}
<div>未支付<br></div>
{% elif v.status == 1%}
<div>已支付<br></div>
{% endif %}

</div>
</td>
<td class="w125 center opreat b-t hidden-xs hidden-sm">
<ul>
<li class="more"><a href="#" target="_blank">查看详情</a></li>
</ul>
</td>
</tr>
{% endfor %}
</tbody>
</table>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
{% endblock %}

进入myadmin/AdminMiddleware.py,并在def __call__(self, request)增加中间件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#前台登录的中间件
homeurl = [
reverse('myhome_cart_add'),
reverse('myhome_cart_index'),
reverse('myhome_cart_del'),
reverse('myhome_cart_clear'),
reverse('myhome_cart_edit'),
reverse('myhome_order_confirm'),
reverse('myhome_order_create'),
reverse('myhome_order_pay'),
reverse('myhome_center_order'),

]

if path in homeurl:
# 检测是否已经登录
VipUser = request.session.get('VipUser', None)
if not VipUser:
# 没有登录
return HttpResponse('<script>alert("请先登录");location.href="' + reverse('myhome_login') + '";</script>')