03-商品功能的设计

一 商品模型的设计

  • 商品的模型设计,包含商品的数据设计,商品的类型。

  • TypeInfo模型类中有title,isDelete等

  • GoodsInfo包含商品的标题,图片,价格,是否删除,重量,点击量,简等

    model.py
    from django.db import models
    from tinymce.models import HTMLField
    class TypeInfo(models.Model):
      ttitle = models.CharField(max_length=20)
      isDelete = models.BooleanField(default=False)
      def __str__(self):
          return self.ttitle.encode('utf-8')
    class GoodsInfo(models.Model):
      gtitle = models.CharField(max_length=20)
      gpic = models.ImageField(upload_to='df_goods')
      gprice = models.DecimalField(max_digits=5,decimal_places=2)
      isDelete = models.BooleanField(default=False)
      gunit = models.CharField(max_length=20,default='500g')
      gclick =models.IntegerField()
      gjianjie = models.CharField(max_length=200)
      gkucun = models.IntegerField()
      gcontent = HTMLField()
      gtype = models.ForeignKey(TypeInfo)
    
      def __str__(self):
          return  self.gtitle.encode('utf-8')
    

二 商品视图设计

  • 商品视图包括商品首页,商品列表页,商品详情页等三个页面的设计

      viwes.py
    from django.shortcuts import render
    
    from .models import TypeInfo, GoodsInfo
    
    from django.core.paginator import Paginator
    def index(request):
      count = request.session.get('count')
      fruit = GoodsInfo.objects.filter(gtype__id=2).order_by("-id")[:4]
      fruit2 = GoodsInfo.objects.filter(gtype__id=2).order_by("-gclick")[:4]
      fish = GoodsInfo.objects.filter(gtype__id=4).order_by("-id")[:4]
      fish2 = GoodsInfo.objects.filter(gtype__id=4).order_by("-gclick")[:3]
      meat = GoodsInfo.objects.filter(gtype__id=1).order_by("-id")[:4]
      meat2 = GoodsInfo.objects.filter(gtype__id=1).order_by("-gclick")[:4]
      egg = GoodsInfo.objects.filter(gtype__id=5).order_by("-id")[:4]
      egg2 = GoodsInfo.objects.filter(gtype__id=5).order_by("-gclick")[:4]
      vegetables = GoodsInfo.objects.filter(gtype__id=3).order_by("-id")[:4]
      vegetables2 = GoodsInfo.objects.filter(gtype__id=3).order_by("-gclick")[:4]
      frozen = GoodsInfo.objects.filter(gtype__id=6).order_by("-id")[:4]
      frozen2 = GoodsInfo.objects.filter(gtype__id=6).order_by("-gclick")[:4]
    
      context = {'title': '首页', 'fruit': fruit,
                 'fish': fish, 'meat': meat,
                 'egg': egg,
                 'vegetables': vegetables,
                 'frozen': frozen,
                 'fruit2': fruit2, 'fish2': fish2,
                 'meat2': meat2,
                 'egg2': egg2,
                 'vegetables2': vegetables2,
                 'frozen2': frozen2,
                 'guest_cart': 1, 'page_name': 0,
                 'count': count}
    
      return render(request , 'df_goods/index.html', context)
    
    def goodlist(request,typeid,pageid,sort):
      count = request.session.get('count')
    
      newgood = GoodsInfo.objects.all().order_by('-id')[:2]
      if sort =='1':
          sumGoodList = GoodsInfo.objects.filter(
              gtype_id = typeid).order_by('id')
      if sort=='2':
          sumGoodList = GoodsInfo.objects.filter(
              gtype_id = typeid).order_by('gprice')
    
      if sort =='3':
          sumGoodList = GoodsInfo.objects.filter(gtype_id=typeid).order_by('-gclick')
      paginator = Paginator(sumGoodList,15)
      goodList = paginator.page(int(pageid))
      pindexlist = paginator.page_range
    
      goodtype = TypeInfo.objects.get(id = typeid)
    
      context = {'title': '商品详情', 'list': 1,
                 'guest_cart': 1, 'goodtype': goodtype,
                 'newgood': newgood, 'goodList': goodList,
                 'typeid': typeid, 'sort': sort,
                 'pindexlist': pindexlist, 'pageid': int(pageid), 'count': count}
    
      # 渲染返回结果
      return render(request, 'df_goods/list.html', context)
    
    def detail(request,id):
      goods = GoodsInfo.objects.get(pk = int(id))
      goods.gclick = goods.gclick+1
      goods.save()
    
      goodtype = goods.gtype
    
      count = request.session.get('count')
      news = goods.gtype.goodsinfo_set.order_by('-id'[0:2])
    
      context = {'title': goods.gtype.ttitle, 'guest_cart': 1,
                 'g': goods, 'newgood': news, 'id': id,
                 'isDetail': True, 'list': 1, 'goodtype': goodtype, 'count': count}
      response = render(request, 'df_goods/detail.html', context)
    
      goods_ids = request.COOKIES.get('good_ids''')
      goods_id = '%d'%goods_ids
    
      if goods_ids!="":
          goods_id_list = goods_ids.split(',')
          if goods_id_list.count(goods_id)>=1:
              goods_id_list.remove(goods_id)
    
          goods_id_list.insert(0,goods_id)
    
          if len(goods_id_list) >= 6:
              # 超过五个则删除第6个
              del goods_id_list[5]
              # 添加商品id到cookies
          goods_ids = ','.join(goods_id_list)
      else:
          # 第一次添加,直接追加
              goods_ids = goods_id
      response.set_cookie('goods_ids', goods_ids)
    
      return response
    

你可能感兴趣的:(03-商品功能的设计)