当前位置:   article > 正文

【Django】网上蛋糕商城后台-商品管理

【Django】网上蛋糕商城后台-商品管理

1.商品管理功能

当管理员点击商品管理时,发送服务器请求

path('admin/goods_list/', viewsAdmin.goods_list),
  1. # 处理商品列表请求
  2. def goods_list(request):
  3. try:
  4. type = request.GET["type"]
  5. except:
  6. type = 0
  7. try:
  8. ym = request.GET["ym"]
  9. except:
  10. ym = 1
  11. if int(type) == 0:
  12. goodsList = Goods.objects.all().order_by("-id").values()
  13. for goods in goodsList:
  14. typeName = Type.objects.get(id=goods["type_id"]).name
  15. types = Recommend.objects.filter(goods_id=goods["id"]).values_list("type")
  16. if (1,) in types:
  17. goods["isScroll"] = True
  18. if (2,) in types:
  19. goods["isHot"] = True
  20. if (3,) in types:
  21. goods["isNew"] = True
  22. goods["typeName"] = typeName
  23. else:
  24. recommends = Recommend.objects.filter(type=type).order_by("-goods_id")
  25. goodsList = []
  26. for r in recommends:
  27. # 根据推荐栏类型查询商品信息,将查询的对象转换成字典
  28. goods = Goods.objects.get(id=r.goods_id).__dict__
  29. # 根据商品id查询该商品添加在哪些推荐栏中
  30. types = Recommend.objects.filter(goods_id=r.goods_id).values_list("type")
  31. if (1,) in types:
  32. goods["isScroll"] = True
  33. if (2,) in types:
  34. goods["isHot"] = True
  35. if (3,) in types:
  36. goods["isNew"] = True
  37. # 根据商品中的分类id查询分类名称
  38. typeName = Type.objects.get(id=goods["type_id"])
  39. goods["typeName"] = typeName.name
  40. goodsList.append(goods)
  41. # 将该分类的商品信息进行分页处理,每页显示6条记录
  42. pag = paginator.Paginator(goodsList, 6)
  43. # 根据当前页码获取当前分页信息
  44. pageInfo = pag.get_page(ym)
  45. # 获取当前页的商品列表信息
  46. goodsList = pageInfo.object_list
  47. # 获取总页码数
  48. yms = pag.page_range
  49. return render(request, "adminTemp/goods_list.html",
  50. {"goodsList": goodsList, "page": pageInfo, "yms": yms, "type": type})
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <title>商品列表</title>
  5. {% load static %}
  6. <meta charset="utf-8"/>
  7. <link rel="stylesheet" href="{% static 'css/bootstrap.css' %}"/>
  8. <link rel="stylesheet" href="{% static 'css/page.css' %}"/>
  9. </head>
  10. <body>
  11. <div class="container-fluid">
  12. {% include "adminTemp/header.html" %}
  13. <div class="text-right"><a class="btn btn-warning" href="/admin/goods_add/">添加商品</a></div>
  14. <br>
  15. <ul role="tablist" class="nav nav-tabs">
  16. <li {% if type == 0 %}
  17. class="active"
  18. {% endif %} role="presentation"><a href="/admin/goods_list/">全部商品</a></li>
  19. <li {% if type == 1 %}
  20. class="active"
  21. {% endif %} role="presentation"><a href="/admin/goods_list/?type=1">条幅推荐</a></li>
  22. <li {% if type == 2 %}
  23. class="active"
  24. {% endif %} role="presentation"><a href="/admin/goods_list/?type=2">热销推荐</a></li>
  25. <li {% if type == 3 %}
  26. class="active"
  27. {% endif %} role="presentation"><a href="/admin/goods_list/?type=3">新品推荐</a></li>
  28. </ul>
  29. <br>
  30. <table class="table table-bordered table-hover">
  31. <tr>
  32. <th width="5%">ID</th>
  33. <th width="10%">图片</th>
  34. <th width="10%">名称</th>
  35. <th width="20%">介绍</th>
  36. <th width="10%">价格</th>
  37. <th width="10%">类目</th>
  38. <th width="25%">操作</th>
  39. </tr>
  40. {% for g in goodsList %}
  41. <tr>
  42. <td><p>{{ g.id }}</p></td>
  43. <td><p><a href="/goods_detail/?id={{ g.id }}" target="_blank"><img src="{% static g.cover %}"
  44. width="100px"
  45. height="100px"></a></p></td>
  46. <td><p><a href="/goods_detail/?id={{ g.id }}" target="_blank">{{ g.name }}</a></p></td>
  47. <td><p>{{ g.intro }}</p></td>
  48. <td><p>{{ g.price }}</p></td>
  49. <td><p>{{ g.typeName }}</p></td>
  50. <td>
  51. <p>
  52. {% if g.isScroll %}
  53. <a class="btn btn-info"
  54. href="/admin/goods_recommend/?id={{ g.id }}&method=remove&typeTarget=1">移出条幅</a>
  55. {% endif %}
  56. {% if not g.isScroll %}
  57. <a class="btn btn-primary"
  58. href="/admin/goods_recommend/?id={{ g.id }}&method=add&typeTarget=1">加入条幅</a>
  59. {% endif %}
  60. {% if g.isHot %}
  61. <a class="btn btn-info"
  62. href="/admin/goods_recommend/?id={{ g.id }}&method=remove&typeTarget=2">移出热销</a>
  63. {% endif %}
  64. {% if not g.isHot %}
  65. <a class="btn btn-primary"
  66. href="/admin/goods_recommend/?id={{ g.id }}&method=add&typeTarget=2">加入热销</a>
  67. {% endif %}
  68. {% if g.isNew %}
  69. <a class="btn btn-info"
  70. href="/admin/goods_recommend/?id={{ g.id }}&method=remove&typeTarget=3">移出新品</a>
  71. {% endif %}
  72. {% if not g.isNew %}
  73. <a class="btn btn-primary"
  74. href="/admin/goods_recommend/?id={{ g.id }}&method=add&typeTarget=3">加入新品</a>
  75. {% endif %}
  76. </p>
  77. <a class="btn btn-success"
  78. href="/admin/goods_editshow/?id={{ g.id }}&ym={{ page.number }}">修改</a>
  79. <a class="btn btn-danger"
  80. href="/admin/goods_delete/?id={{ g.id }}&ym={{ page.number }}">删除</a>
  81. </td>
  82. </tr>
  83. {% endfor %}
  84. </table>
  85. <br>
  86. <!-- 显示页码导航栏 -->
  87. <div id="nav" align="center">
  88. <!-- 上一页 -->
  89. <!-- 判断当前页是否有上一页,如果有上一页则显示上一页的按钮,否则就不显示上一页 -->
  90. {% if page.has_previous %}
  91. <a href="/admin/goods_list/?ym={{ page.previous_page_number }}&type={{ type }}" class="up_page">上一页</a>
  92. {% endif %}
  93. <!-- 页码 -->
  94. {% for ym in yms %}
  95. {% if page.number == ym %}
  96. <a href="/admin/goods_list/?ym={{ ym }}&type={{ type }}" class="p_page c_page">{{ ym }}</a>
  97. {% else %}
  98. <a href="/admin/goods_list/?ym={{ ym }}&type={{ type }}" class="p_page">{{ ym }}</a>
  99. {% endif %}
  100. {% endfor %}
  101. <!-- 下一页 -->
  102. {% if page.has_next %}
  103. <a href="/admin/goods_list/?ym={{ page.next_page_number }}&type={{ type }}" class="do_page">下一页</a>
  104. {% endif %}
  105. </div>
  106. <br>
  107. </div>
  108. </body>
  109. </html>

2.加入或移除推荐栏功能

选择某个商品,点击加入条幅,移除条幅,加入热销,移除热销,加入新品,移除新品按钮时,触发请求事件,传递不同参数信息给服务器

path('admin/goods_recommend/',viewsAdmin.goods_recommend),
  1. # 处理商品的推荐栏请求
  2. def goods_recommend(request):
  3. id = request.GET["id"]
  4. method = request.GET["method"]
  5. typeTarget = request.GET["typeTarget"]
  6. if "add" == method:
  7. # 添加至推荐栏
  8. Recommend.objects.create(goods_id=id, type=typeTarget)
  9. elif "remove" == method:
  10. # 从推荐栏中移除
  11. r = Recommend.objects.get(goods_id=id, type=typeTarget)
  12. r.delete()
  13. # 刷新商品管理列表页面
  14. return redirect(goods_list)

3.添加商品功能

当管理员点击添加商品按钮,触发请求事件

path('admin/goods_add/', viewsAdmin.goods_add),
  1. # 处理添加商品页面的跳转请求
  2. def goods_add(request):
  3. types = Type.objects.all()
  4. return render(request, "adminTemp/goods_add.html", {"typeList": types})
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <title>商品添加</title>
  5. {% load static %}
  6. <meta charset="utf-8"/>
  7. <link rel="stylesheet" href="{% static 'css/bootstrap.css' %}"/>
  8. </head>
  9. <body>
  10. <div class="container-fluid">
  11. {% include "adminTemp/header.html" %}
  12. <br><br>
  13. <form class="form-horizontal" action="/admin/addGoods/" method="post" enctype="multipart/form-data">
  14. {% csrf_token %}
  15. <div class="form-group">
  16. <label for="input_name" class="col-sm-1 control-label">名称</label>
  17. <div class="col-sm-6">
  18. <input type="text" class="form-control" id="input_name" name="name" required="required">
  19. </div>
  20. </div>
  21. <div class="form-group">
  22. <label for="input_name" class="col-sm-1 control-label">价格</label>
  23. <div class="col-sm-6">
  24. <input type="text" class="form-control" id="input_name" name="price">
  25. </div>
  26. </div>
  27. <div class="form-group">
  28. <label for="input_name" class="col-sm-1 control-label">介绍</label>
  29. <div class="col-sm-6">
  30. <input type="text" class="form-control" id="input_name" name="intro">
  31. </div>
  32. </div>
  33. <div class="form-group">
  34. <label for="input_name" class="col-sm-1 control-label">库存</label>
  35. <div class="col-sm-6">
  36. <input type="text" class="form-control" id="input_name" name="stock">
  37. </div>
  38. </div>
  39. <div class="form-group">
  40. <label for="input_file" class="col-sm-1 control-label">封面图片</label>
  41. <div class="col-sm-6">
  42. <input type="file" name="cover" id="input_file" required="required">推荐尺寸: 500 * 500
  43. </div>
  44. </div>
  45. <div class="form-group">
  46. <label for="input_file" class="col-sm-1 control-label">详情图片1</label>
  47. <div class="col-sm-6">
  48. <input type="file" name="image1" id="input_file" required="required">推荐尺寸: 500 * 500
  49. </div>
  50. </div>
  51. <div class="form-group">
  52. <label for="input_file" class="col-sm-1 control-label">详情图片2</label>
  53. <div class="col-sm-6">
  54. <input type="file" name="image2" id="input_file" required="required">推荐尺寸: 500 * 500
  55. </div>
  56. </div>
  57. <div class="form-group">
  58. <label for="select_topic" class="col-sm-1 control-label">类目</label>
  59. <div class="col-sm-6">
  60. <select class="form-control" id="select_topic" name="typeid">
  61. {% for t in typeList %}
  62. <option value="{{ t.id }}">{{ t.name }}</option>
  63. {% endfor %}
  64. </select>
  65. </div>
  66. </div>
  67. <div class="form-group">
  68. <div class="col-sm-offset-1 col-sm-10">
  69. <button type="submit" class="btn btn-success">提交保存</button>
  70. </div>
  71. </div>
  72. </form>
  73. </div>
  74. </body>
  75. </html>

当管理员填写完商品信息以及选择好上传的图片后,点击提交保存按钮,将表单提交给服务器

path('admin/addGoods/',viewsAdmin.addGoods),
  1. # 获取商品添加请求
  2. def addGoods(request):
  3. name = request.POST["name"]
  4. price = request.POST["price"]
  5. intro = request.POST["intro"]
  6. stock = request.POST["stock"]
  7. pic = request.FILES.getlist('cover')
  8. cover = upload(pic[0])
  9. pic = request.FILES.getlist('image1')
  10. image1 = upload(pic[0])
  11. pic = request.FILES.getlist('image2')
  12. image2 = upload(pic[0])
  13. typeid = request.POST["typeid"]
  14. Goods.objects.create(name=name, price=price, intro=intro, stock=stock, cover=cover, image1=image1, image2=image2,
  15. type_id=typeid)
  16. # 添加成功刷新商品管理列表页面
  17. return redirect(goods_list)

对于处理图片上传的函数如下

  1. def upload(pic, image=None):
  2. # 指定文件上传路径
  3. path = "CookieShopClient/static/picture/"
  4. if image:
  5. file_path = path + str(image)[8:]
  6. # 检查文件是否存在
  7. if os.path.isfile(file_path):
  8. # 删除文件
  9. os.remove(file_path)
  10. imageName = ""
  11. # 检查文件夹是否存在
  12. if not os.path.exists(path):
  13. # 如果文件夹不存在,则创建它
  14. os.makedirs(path)
  15. imageName = pic.name
  16. # 获取当前时间的时间戳(秒)
  17. timestamp_seconds = time.time()
  18. # 转换为毫秒
  19. timestamp_milliseconds = int(timestamp_seconds * 1000)
  20. imageName = str(imageName).split(".")[0] + str(timestamp_milliseconds) + "." + str(imageName).split(".")[1]
  21. url = path + imageName
  22. with open(url, 'wb') as f:
  23. for data in pic.chunks():
  24. f.write(data)
  25. return "/picture/" + imageName

4.修改商品功能

当管理员选择某个商品进行修改时,将该商品的商品编号以及所在分页页码发送给修改页面

path('admin/goods_editshow/',viewsAdmin.goods_editshow),
  1. # 处理跳转至修改页面的请求
  2. def goods_editshow(request):
  3. id = request.GET["id"]
  4. ym = request.GET["ym"]
  5. goods = Goods.objects.get(id=id)
  6. # 查询该商品所属分类
  7. typeName = Type.objects.get(id=goods.type_id).name
  8. types = Type.objects.all()
  9. return render(request, "adminTemp/goods_edit.html", {"g": goods, "typeName": typeName, "ym": ym, "typeList": types})
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <title>商品编辑</title>
  5. {% load static %}
  6. <meta charset="utf-8" />
  7. <link rel="stylesheet" href="{% static 'css/bootstrap.css' %}" />
  8. </head>
  9. <body>
  10. <div class="container-fluid">
  11. {% include "adminTemp/header.html" %}
  12. <br><br>
  13. <form class="form-horizontal" action="/admin/goods_edit/" method="post" enctype="multipart/form-data">
  14. {% csrf_token %}
  15. <input type="hidden" name="id" value="{{ g.id }}"/>
  16. <input type="hidden" name="cover" value="{{ g.cover }}"/>
  17. <input type="hidden" name="image1" value="{{ g.image1 }}"/>
  18. <input type="hidden" name="image2" value="{{ g.image2 }}"/>
  19. <input type="hidden" name="ym" value="{{ ym }}"/>
  20. <input type="hidden" name="type" value="{{ typeName }}"/>
  21. <div class="form-group">
  22. <label for="input_name" class="col-sm-1 control-label">名称</label>
  23. <div class="col-sm-6">
  24. <input type="text" class="form-control" id="input_name" name="name" value="{{ g.name }}" required="required">
  25. </div>
  26. </div>
  27. <div class="form-group">
  28. <label for="input_name" class="col-sm-1 control-label">价格</label>
  29. <div class="col-sm-6">
  30. <input type="text" class="form-control" id="input_name" name="price" value="{{ g.price }}">
  31. </div>
  32. </div>
  33. <div class="form-group">
  34. <label for="input_name" class="col-sm-1 control-label">介绍</label>
  35. <div class="col-sm-6">
  36. <input type="text" class="form-control" id="input_name" name="intro" value="{{ g.intro }}">
  37. </div>
  38. </div>
  39. <div class="form-group">
  40. <label for="input_name" class="col-sm-1 control-label">库存</label>
  41. <div class="col-sm-6">
  42. <input type="text" class="form-control" id="input_name" name="stock" value="{{ g.stock }}">
  43. </div>
  44. </div>
  45. <div class="form-group">
  46. <label for="input_file" class="col-sm-1 control-label">封面图片</label>
  47. <div class="col-sm-6"><img src="{% static g.cover %}" width="100" height="100"/>
  48. <input type="file" name="cover" id="input_file">推荐尺寸: 500 * 500
  49. </div>
  50. </div>
  51. <div class="form-group">
  52. <label for="input_file" class="col-sm-1 control-label">详情图片1</label>
  53. <div class="col-sm-6"><img src="{% static g.image1 %}" width="100" height="100"/>
  54. <input type="file" name="image1" id="input_file">推荐尺寸: 500 * 500
  55. </div>
  56. </div>
  57. <div class="form-group">
  58. <label for="input_file" class="col-sm-1 control-label">详情图片2</label>
  59. <div class="col-sm-6"><img src="{% static g.image2 %}" width="100" height="100"/>
  60. <input type="file" name="image2" id="input_file">推荐尺寸: 500 * 500
  61. </div>
  62. </div>
  63. <div class="form-group">
  64. <label for="select_topic" class="col-sm-1 control-label">类目</label>
  65. <div class="col-sm-6">
  66. <select class="form-control" id="select_topic" name="typeid">
  67. {% for t in typeList %}
  68. <option
  69. {% if t.id == g.type_id %}
  70. selected="selected"
  71. {% endif %}
  72. value="{{ t.id }}">{{ t.name }}</option>
  73. {% endfor %}
  74. </select>
  75. </div>
  76. </div>
  77. <div class="form-group">
  78. <div class="col-sm-offset-1 col-sm-10">
  79. <button type="submit" class="btn btn-success">提交修改</button>
  80. </div>
  81. </div>
  82. </form>
  83. </div>
  84. </body>
  85. </html>

当管理员修改信息以及重新选择新图片后,将表单提交给服务器,服务器通过比较原图片以及新图片地址判断当前图片是否需要重新上传

path('admin/goods_edit/',viewsAdmin.goods_edit),
  1. # 处理修改商品信息的请求
  2. def goods_edit(request):
  3. id = request.POST["id"]
  4. # 根据id查询出原商品信息
  5. goods = Goods.objects.filter(id=id)
  6. name = request.POST["name"]
  7. price = request.POST["price"]
  8. intro = request.POST["intro"]
  9. stock = request.POST["stock"]
  10. try:
  11. # 判断修改页面传递的图片名称是否和数据库中原本图片名称一致,如果一致表示该图片没有被修改
  12. # 如果图片没有修改,则返回值为空
  13. pic = request.FILES.getlist('cover')[0]
  14. cover = upload(pic, goods[0].cover)
  15. except:
  16. cover = goods[0].cover
  17. try:
  18. # 判断修改页面传递的图片名称是否和数据库中原本图片名称一致,如果一致表示该图片没有被修改
  19. # 如果图片没有修改,则返回值为空
  20. pic = request.FILES.getlist('image1')[0]
  21. image1 = upload(pic, goods[0].image1)
  22. except:
  23. image1 = goods[0].image1
  24. try:
  25. # 判断修改页面传递的图片名称是否和数据库中原本图片名称一致,如果一致表示该图片没有被修改
  26. # 如果图片没有修改,则返回值为空
  27. pic = request.FILES.getlist('image2')[0]
  28. image2 = upload(pic, goods[0].image2)
  29. except:
  30. image2 = goods[0].image2
  31. typeid = request.POST["typeid"]
  32. ym = request.POST["ym"]
  33. # 修改商品信息
  34. goods.update(name=name, price=price, intro=intro, stock=stock, cover=cover, image1=image1, image2=image2,
  35. type_id=typeid)
  36. return HttpResponseRedirect("/admin/goods_list/?ym=" + ym)

5.删除商品功能

当管理员选择某个商品删除的时候,触发请求

path('admin/goods_delete/',viewsAdmin.goods_delete),
  1. # 处理删除商品的请求
  2. def goods_delete(request):
  3. id=request.GET["id"]
  4. ym=request.GET["ym"]
  5. # 先查看当前商品是否有被添加为推荐栏商品,如果有,需要先从推荐栏中删除
  6. rs=Recommend.objects.filter(goods_id=id)
  7. if rs:
  8. rs.delete()
  9. # 根据商品编号将商品信息查询出来
  10. goods=Goods.objects.get(id=id)
  11. remove_image(goods.cover)
  12. remove_image(goods.image1)
  13. remove_image(goods.image2)
  14. goods.delete()
  15. return HttpResponseRedirect("/admin/goods_list/?ym=" + ym)
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小舞很执着/article/detail/897286
推荐阅读
相关标签
  

闽ICP备14008679号