改善前台中间件
进入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): 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) 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): 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)
context = {'orderslist': orderslist}
return render(request, 'myadmin/order/index.html', context)
def orders_set_status(request): 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): 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 %}
|