改善前台中间件

进入templates/myhome/index/info.html,并修改加入购物车单击事件

1
2
3
4
5
6
7
8
9
10
$('#J_btnAddCart').click(function(){
// 检测是否登录
var isLogin = '{{ request.session.VipUser.uid }}'
if(!isLogin){
alert('请先登录');
var hhref = window.location.href;
var indexId = hhref.indexOf('?');
var substr = hhref.substring(indexId);
location.href="{% url 'myhome_login' %}"+"?nexturl="+window.location.pathname+substr;
}

进入myhome/views/LoginViews.py,修改myhome_login、myhome_dologin函数

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
# 显示登录页面
def myhome_login(request):
try:
urlstr = request.GET['urlstr']
print(urlstr)
context = {'urlstr': urlstr}
return render(request, 'myhome/login/login.html', context)
except:
pass
# 加载页面
return render(request,'myhome/login/login.html')

# 执行登录
def myhome_dologin(request):
# 验证手机号 用户是否存在
try:
ob = Users.objects.get(phone=request.POST['phone'])
urlstr = request.POST['urlstr']
# 验证密码
res = check_password(request.POST['password'],ob.password)
if res:
# 验证成功
request.session['VipUser'] = {'uid': ob.id, 'nikename': ob.nikename, 'phone': ob.phone,'pic_url': ob.pic_url}
if urlstr:
return HttpResponse('<script>alert("登录成功");location.href="'+urlstr+'";</script>')
return HttpResponse('<script>alert("登录成功");location.href="'+reverse('myhome_index')+'";</script>')
except:
pass
return HttpResponse('<script>alert("手机号或密码不正确");history.back(-1);</script>')

进入templates/myhome/login/login.html,并在form表单加入隐藏input

1
<input class="hidden" name="urlstr" value={{ urlstr }}>

后台商品列表的编辑

进入myadmin/urls.py,增加路径

1
2
url(r'^goods/edit/$', GoodsViews.goods_edit, name="myadmin_goods_edit"),
url(r'^goods/setstatus/$',GoodsViews.goods_set_status,name="goods_set_status"),

进入myadmin/views/GoodsViews.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
29
30
31
32
33
34
35
36
37
38
39
40
def goods_edit(request):
# 接受商品id
uid = request.GET.get('uid')
# 获取当前会员对象
gd = Goods.objects.get(id=uid)

# 判断当前的请求方式
if request.method == 'POST':

# 判断头像是否更新
myfile = request.FILES.get('pic', None)
if myfile:
# 如果有新头像上传,则先删除原头像图片,
os.remove(BASE_DIR + gd.pic_url)
# /static/uploads/1545033786.7237682.png
# ./static/uploads/1545033786.7237682.png
# /home/yc/py16/py16-project/web
# 在上传新的头像
gd.pic_url = uploads_pic(myfile)

# 更新其它字段
gd.goodsname = request.POST.get('goodsname')
gd.title = request.POST.get('title')
gd.price = request.POST.get('price')
gd.goodsnum = request.POST.get('goodsnum')
gd.goodsinfo = request.POST.get('goodsinfo')
gd.save()
return HttpResponse('<script>alert("更新成功");location.href="' + reverse('myadmin_goods_index') + '";</script>')

else:
# 显示编辑表单
context = {'uinfo': gd}
return render(request, 'myadmin/goods/edit.html', context)

def goods_set_status(request):
# 通过uid获取当前商品对象
ob = Goods.objects.get(id=request.GET.get('uid'))
ob.status = request.GET.get('status')
ob.save()
return JsonResponse({'msg': '状态更新成功', 'code': 0})

进入templates/myadmin/goods/index.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
{% block con %}
<div class="row-content am-cf">
<div class="row">
<div class="am-u-sm-12 am-u-md-12 am-u-lg-12">
<div class="widget am-cf">
<div class="widget-head am-cf">
<div class="widget-title am-cf">商品列表</div>

</div>
<div class="widget-body am-fr">

<div class="am-u-sm-12 am-u-md-6 am-u-lg-6">
<div class="am-form-group">
<div class="am-btn-toolbar">
<div class="am-btn-group am-btn-group-xs">
<a href="{% url 'myadmin_goods_add' %}" class="am-btn am-btn-default am-btn-success"><span class="am-icon-plus"></span> 新增</a>
</div>
</div>
</div>
</div>
<form>
<div class="am-u-sm-12 am-u-md-6 am-u-lg-3">
<div class="am-form-group tpl-table-list-select">
<select name="types" data-am-selected="{btnSize: 'sm'}" style="display: none;">
<option value="all" {% if request.GET.types == 'all' %} selected {% endif %}>全局搜索</option>
<option value="goodsname" {% if request.GET.types == 'goodsname' %} selected {% endif %}>商品名</option>
<option value="id" {% if request.GET.types == 'id' %} selected {% endif %}>ID</option>
<option value="title" {% if request.GET.types == 'title' %} selected {% endif %}>标题</option>
</select>
</div>
</div>
<div class="am-u-sm-12 am-u-md-12 am-u-lg-3">
<div class="am-input-group am-input-group-sm tpl-form-border-form cl-p">
<input type="text" name="keywords" value="{{ request.GET.keywords }}" class="am-form-field ">
<span class="am-input-group-btn">
<button class="am-btn am-btn-default am-btn-success tpl-table-list-field am-icon-search"></button>
</span>
</div>
</div>
</form>


<div class="am-u-sm-12">
<table width="100%" class="am-table am-table-compact am-table-striped tpl-table-black ">
<thead>
<tr>
<th>ID</th>
<th>缩略图</th>
<th>商品名</th>
<th>标题</th>
<th>所属分类</th>
<th>价格</th>
<th>库存</th>
<th>销量</th>
<th>点击量</th>
<th>状态</th>
<th>注册时间</th>
<th>操作</th>
</tr>
</thead>
<tbody>
{% for v in goodslist %}
<tr>
<td class="am-text-middle">{{ v.id }}</td>
<td>
<img src="{{ v.pic_url }}" class="tpl-table-line-img" alt="">
</td>
<td class="am-text-middle">{{ v.goodsname }}</td>
<td class="am-text-middle">{{ v.title }}</td>
<td class="am-text-middle">{{ v.cateid.name }}</td>
<td class="am-text-middle">{{ v.price }}</td>
<td class="am-text-middle">{{ v.goodsnum }}</td>
<td class="am-text-middle">{{ v.ordernum }}</td>
<td class="am-text-middle">{{ v.clicknum }}</td>
<td class="am-text-middle">
<select uid="{{ v.id }}" class="select-status" style="color: black">
<option value="0" {% if v.status == 0 %} selected {% endif %}>新品</option>
<option value="1" {% if v.status == 1 %} selected {% endif %}>推荐</option>
<option value="1" {% if v.status == 2 %} selected {% endif %}>下架</option>
</select>
</td>
<td class="am-text-middle">{{ v.addtime }}</td>
<td class="am-text-middle">
<div class="tpl-table-black-operation">
<a href="{% url 'myadmin_goods_edit' %}?uid={{ v.id }}">
<i class="am-icon-pencil"></i> 编辑
</a>
</div>
</td>
</tr>
{% endfor %}

<!-- more data -->
</tbody>
</table>
</div>
<div class="am-u-lg-12 am-cf">

<div class="am-fr">
<ul class="am-pagination tpl-pagination">
<!-- <li class="am-disabled"><a href="#">«</a></li> -->
<!-- <li class="am-active"><a href="#">1</a></li> -->
{% load pagetag %}
{% showpage goodslist.paginator.num_pages request %}
<!-- <li><a href="#">»</a></li> -->
</ul>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
{<script type="text/javascript">
$('.select-status').change(function(){
// 获取当前元素的 值
var status = $(this).val()
// 获取当前元素的 id
var uid = $(this).attr('uid')

// 发送ajax请求.修改状态
$.get('{% url 'goods_set_status' %}',{'uid':uid,'status':status},function(data){
// 判断当前的返回值
if(data['code'] == 0){
alert(data['msg'])
}
},'json')
})
</script>
{% endblock %}

订单列表

进入myadmin/urls.py,增加路径

1
2
3
4
url(r'^order/index/$', OrderViews.order_index, name="order_index"),
url(r'^orders/setstatus/$',OrderViews.orders_set_status,name="orders_set_status"),
url(r'^orders/setpaytype/$', OrderViews.orders_set_paytype, name="orders_set_paytype"),
url(r'^orders/delete/$', OrderViews.myadmin_order_del, name="myadmin_order_del"),

进入myadmin/views,创建OrderViews.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
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
from django.shortcuts import render, reverse
from django.http import HttpResponse,JsonResponse
from .. models import Order

def order_index(request):
# 获取所有的商品对象
data = Order.objects.all()

# 获取搜索条件
types = request.GET.get('types', None)
keywords = request.GET.get('keywords', None)
# 判断是否搜索
if types == 'all':
from django.db.models import Q
data = data.filter(Q(id__contains=keywords) | Q(username__contains=keywords) | Q(phone__contains=keywords))
elif types:
search = {types + '__contains': keywords}
data = data.filter(**search)

# 导入分页类
from django.core.paginator import Paginator
# 实例化分页类,第二个参数是每页多少条
p = Paginator(data, 15)
# 获取当前的页码数
pageindex = request.GET.get('page', 1)
# 获取当前页的数据
orderslist = p.page(pageindex)

# 获取所有的页码
# p.count 一共多少条
# pages = p.page_range #迭代器(range(,))
# pages = p.num_pages #分了多少页

context = {'orderslist': orderslist}

# 加载模板
return render(request, 'myadmin/order/index.html', context)

def orders_set_status(request):
# 通过uid获取当前商品对象
ob = Order.objects.get(id=request.GET.get('uid'))
ob.status = request.GET.get('status')
ob.save()
return JsonResponse({'msg': '支付状态更新成功', 'code': 0})

def orders_set_paytype(request):
# 通过uid获取当前商品对象
ob = Order.objects.get(id=request.GET.get('uid'))
ob.paytype = request.GET.get('paytype')
ob.save()
return JsonResponse({'msg': '支付方式状态更新成功', 'code': 0})

def myadmin_order_del(request):
oid = request.GET.get('oid')
# 判断当前分类下是否还有子类
res = Order.objects.get(id=oid)
res.delete()
return JsonResponse({'msg': '删除成功', 'code': 0})

进入templates/myadmin,创建order/index.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
{% extends 'myadmin/index.html' %}

{% block title %}
<title>后台订单列表</title>
{% endblock %}


{% block con %}
<div class="row-content am-cf">
<div class="row">
<div class="am-u-sm-12 am-u-md-12 am-u-lg-12">
<div class="widget am-cf">
<div class="widget-head am-cf">
<div class="widget-title am-cf">订单列表</div>

</div>
<div class="widget-body am-fr">

<div class="am-u-sm-12 am-u-md-6 am-u-lg-6">
<div class="am-form-group">
<div class="am-btn-toolbar">
<div class="am-btn-group am-btn-group-xs">

</div>
</div>
</div>
</div>
<form>
<div class="am-u-sm-12 am-u-md-6 am-u-lg-3">
<div class="am-form-group tpl-table-list-select">
<select name="types" data-am-selected="{btnSize: 'sm'}" style="display: none;">
<option value="all" {% if request.GET.types == 'all' %} selected {% endif %}>全局搜索</option>
<option value="id" {% if request.GET.types == 'id' %} selected {% endif %}>ID</option>
<option value="username" {% if request.GET.types == 'username' %} selected {% endif %}>收件人</option>
<option value="phone" {% if request.GET.types == 'phone' %} selected {% endif %}>电话</option>

</select>
</div>
</div>
<div class="am-u-sm-12 am-u-md-12 am-u-lg-3">
<div class="am-input-group am-input-group-sm tpl-form-border-form cl-p">
<input type="text" name="keywords" value="{{ request.GET.keywords }}" class="am-form-field ">
<span class="am-input-group-btn">
<button class="am-btn am-btn-default am-btn-success tpl-table-list-field am-icon-search"></button>
</span>
</div>
</div>
</form>


<div class="am-u-sm-12">
<table width="100%" class="am-table am-table-compact am-table-striped tpl-table-black ">
<thead>
<tr>
<th>ID</th>
<th>收件人</th>
<th>电话</th>
<th>地址</th>
<th>总价</th>
<th>支付状态</th>
<th>支付方式</th>
<th>订单时间</th>
<th>会员名</th>
<th>操作</th>
</tr>
</thead>
<tbody>
{% for v in orderslist %}
<tr>
<td class="am-text-middle">{{ v.id }}</td>
<td class="am-text-middle">{{ v.username }}</td>
<td class="am-text-middle">{{ v.phone }}</td>
<td class="am-text-middle">{{ v.address }}</td>
<td class="am-text-middle">{{ v.totalprice }}</td>
<td class="am-text-middle">
<select uid="{{ v.id }}" class="select-status" style="color: black">
<option value="0" {% if v.status == 0 %} selected {% endif %}>未支付</option>
<option value="1" {% if v.status == 1 %} selected {% endif %}>已支付</option>
</select>
</td>
<td class="am-text-middle">
<select uid="{{ v.id }}" class="select-paytype" style="color: black">
<option value="0" {% if v.paytype == 0 %} selected {% endif %}>支付宝</option>
<option value="1" {% if v.paytype == 1 %} selected {% endif %}>其它</option>
</select>
</td>
<td class="am-text-middle">{{ v.addtime }}</td>
<td class="am-text-middle">{{ v.uid.nikename }}</td>
<td class="am-text-middle">
<div class="tpl-table-black-operation">
<a href="javascript:void(0)" cid="{{ v.id }}" class="removeCate tpl-table-black-operation-del">
<i class="am-icon-pencil"></i> 删除
</a>
</div>
</td>
</tr>
{% endfor %}

<!-- more data -->
</tbody>
</table>
</div>
<div class="am-u-lg-12 am-cf">

<div class="am-fr">
<ul class="am-pagination tpl-pagination">
<!-- <li class="am-disabled"><a href="#">«</a></li> -->
<!-- <li class="am-active"><a href="#">1</a></li> -->
{% load pagetag %}
{% showpage orderslist.paginator.num_pages request %}
<!-- <li><a href="#">»</a></li> -->
</ul>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
{<script type="text/javascript">
$('.select-status').change(function(){
// 获取当前元素的 值
var status = $(this).val()
// 获取当前元素的 id
var uid = $(this).attr('uid')

// 发送ajax请求.修改状态
$.get('{% url 'orders_set_status' %}',{'uid':uid,'status':status},function(data){
// 判断当前的返回值
if(data['code'] == 0){
alert(data['msg'])
}
},'json')

})
$('.select-paytype').change(function(){
// 获取当前元素的 值
var paytype = $(this).val()
// 获取当前元素的 id
var uid = $(this).attr('uid')

// 发送ajax请求.修改状态
$.get('{% url 'orders_set_paytype' %}',{'uid':uid,'paytype':paytype},function(data){
// 判断当前的返回值
if(data['code'] == 0){
alert(data['msg'])
}
},'json')

})

// 删除
$('.removeCate').click(function(){
// 获取当前选择的分类的id
var oid = $(this).attr('cid')
var a = $(this)
// 发送ajax请求.到后台执行删除
$.get('{% url 'myadmin_order_del' %}',{'oid':oid},function(data){
// 判断当前的返回值
if(data['code'] == 0){
// 删除成功
// $(this).parents('tr').remove()
// 此处的 $(this) 是谁? ajax对象 XMLHttpRequest
a.parents('tr').remove()
}
alert(data['msg'])
},'json')
})
</script>
{% endblock %}