- 1-1 课程导学
- 2-1 Pycharm的安装和简单使用
- 2-2 MySQL和Navicat的安装和使用
- 2-3 Windows和Linux下安装Python2和Python3
- 2-4 虚拟环境的安装和配置
- 2-5 Vue开发环境搭建
- 2-6 资源获取方式和提问方式
- 3-1 项目初始化
- 3-2 User Model设计
- 3-3 Goods Model设计
- 3-4 Trade交易的Model设计
- 3-5 用户操作的Model设计
- 3-6 migrations原理及表生成
- 3-7 xadmin后台管理系统的配置
- 3-8 导入商品类别数据
- 3-9 导入商品和商品类别数据
- 4-1 restful API介绍
- 4-2 Vue的基本概念介绍
- 4-3 Vue源码结构介绍
- 5-1 Django的view实现商品列表页
- 5-2 Django的serializer序列化model
- 5-3 apiview方式实现商品列表页-1
- 5-4 apiview实现商品列表页-2
- 5-5 drf的modelserial实现商品列表页功能
- 5-6 GenericView方式实现商品列表页和分页功能介绍
- 5-7 viewsets和router完成商品列表项
- 5-8 drf的Apiview、GenericView、Viewset和Router的原理分析
- 5-9 drf的request和response
- 5-10 drf的过滤
- 5-11 drf的搜索和排序
- 5-12 总结
- 6-1 商品列表数据接口-1
- 6-2 商品类别数据接口-2
- 6-3 Vue展示商品分类数据
- 6-4 Vue展示商品列表页数据-1
- 6-5 Vue展示商品列表页数据-2
- 6-6 Vue的商品搜索功能
- 7-1 drf的token登录和原理-1
- 7-2 drf的token登录和原理-2
- 7-3 viewsets配置认证类
- 7-4 json web token的原理
- 7-5 json web token方式完成用户认证
- 7-6 Vue和jwt接口调试
- 7-7 云片网发送短信验证码
- 7-8 drf实现发送短信验证码接口-1
- 7-9 drf实现发送短信验证码接口-2
- 7-10 user serializer和validator验证一1
- 7-11 user serializer和validator验证-2
- 7-12 Django的信号量实现用户密码修改;
- 7-13 Vue和注册功能联调
- 8-1 viewsets实现商品详情页接口
- 8-2 热卖商品接口实现
- 8-3 用户收藏接口实现
- 8-4 drf的权限验证
- 8-5 用户收藏功能和Vue联调
- 9-1 drf的api文档自动生成和功能详解
- 9-2 动态设置serializer和permission获取用户信息
- 9-3 Vue和用户接口信息联调
- 9-4 用户个人信息修改
- 9-5 用户收藏功能
- 9-6 用户留言功能
- 9-7 用户收货地址列表页接口开发
- 9-8 Vue和收货地址接口联调
- 10-1 购物车功能需求分析和加入到购物车实现
- 10-2 修改购物车数量
- 10-3 Vue和购物车接口联调
- 10-4 订单管理接口-1
- 10-5 订单管理接口-2
- 10-6 Vue个人中心订单接口调试
- 10-7 Pycharm远程代码调试-1
- 10-8 Pycharm远程代码调试-2
- 10-9 支付宝公钥、私钥和沙箱环境配置
- 10-10 支付宝开发文档解读
- 10-11 支付宝支付源码解读
- 10-12 支付宝通知接口验证
- 10-13 Django集成支付宝notify_url和return_url接口-1
- 10-14 Django集成支付宝notify_url和return_url接口-2
- 10-15 支付宝接口和Vue联调-1
- 10-16 支付宝接口和Vue联调-2
- 11-1 轮播图接口实现和Vue调试
- 11-2 新品功能接口开发
- 11-3 首页商品分类显示功能-1
- 11-4 首页商品分类显示功能-2
- 11-5 商品点击数、收藏数修改
- 11-6 商品库存和销量修改
- 11-7 drf的缓存设置
- 11-8 drf配置Redis缓存
- 11-9 drf的throttle设置api的访问速率
- 12-1 第三方登录开发模式以及oauth2.0简介
- 12-2 oauth2.0获取微博的access_token
- 12-3 social_django集成第三方登录-1
- 12-4 social_django集成第三方登录-2
- 13-1 sentry的介绍和通过docker搭建sentry
- 13-2 sentry的功能
- 13-3 sentry集成到django rest framework中-1
- 13-4 sentry集成到django rest framework中-2
1-1 课程导学
1.1.1 掌握的技术;
- Vue + Django REST framework前后端分离的技术;
- 彻底玩转restful api开发流程;
- Django REST framework的功能实现和核心源代码分析;
- Sentry完成线上系统的错误日志的监控和告警;
- 第三方登录和支付宝支付的集成;
- 本地调试远程服务器代码的技巧;
1.1.2 看课程
2-1 Pycharm的安装和简单使用
2-2 MySQL和Navicat的安装和使用1
2-3 Windows和Linux下安装Python2和Python3
2.3.1 Python2.x和Python3.x的安装;
2-4 虚拟环境的安装和配置
2.4.1 Virtualenv和virtualenvwrapper工具创建开发环境;
2-5 Vue开发环境搭建
2.5.1 前端开发IDE神器-WebStorm;
2.5.3 Node.js的安装,使用内部的npm;
2.5.4 cnpm淘宝镜像的使用;
2-6 资源获取方式和提问方式
2.6.1 慕课网;
2.6.3 coding和码云;
2.6.4 提问问题方式;
3-1 项目初始化
3.1.1 DjangoREST framework是基于 Django开发的用于 API开发的框架;
django-rest-framework,是一套基于Django 的 REST 框架,是一个强大灵活的构建 Web API 的工具包。
3.1.2 DjangoRESTframework依赖Django;
官方文档:http://www.django-rest-framework.org/
pip install djangorestframework
pip install markdown
pip install django-filter
3.1.3 settings.py中修改数据库指向为为MySQL;
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'MxShop',
'USER': 'root',
'PASSWORD': 'Tqtl911!@#)^',
'HOST': '127.0.0.1',
'PORT': '3306',
'OPTIONS': {'init_command': 'SET default_storage_engine =INNODB;'},
}
}
3.1.4 pip3 install mysqlclient 代替python-mysql;
报错信息:
'Did you install mysqlclient or MySQL-python?' % e django.core.exceptions.ImproperlyConfigured: Error loading MySQLdb module: No module named 'MySQLdb'. Did you install mysqlclient or MySQL-python?
Command "python setup.py egg_info" failed with error code 1 in /private/var/folders/v_/mm78zg6d68j30vn2f3wq3ck40000gn/T/pip-install-0m9q5pa9/mysqlclient/
解决步骤:
- 尝试安装mysqlclient,pip3 install mysqlclient
- 安装pymysql;
- 在__init__.py中添加两行;
import pymysql pymysql.install_as_MySQLdb()
3-2 User Model设计
3.2.1 创建users、goods、trade、user_operation等app,并移动至apps目录下;
3.2.2 进行users.models.py的设计;
3.2.3 settings.py中配置AUTH_USER_MODEL来替代Django本身的AUTH属性;
users.models.py;
from datetime import datetime from django.db import models # Create your models here. from django.contrib.auth.models import AbstractUser class UserProfile(AbstractUser): """ 用户 """ name = models.CharField(max_length=30, null=True, blank=True, verbose_name="姓名") birthday = models.DateField(null=True, blank=True, verbose_name="出生年月") gender = models.CharField(max_length=6, choices=(("male", u"男"), ("female", u"女")), default="female", verbose_name="性别") mobile = models.CharField(max_length=11, verbose_name="电话") email = models.EmailField(max_length=100, null=True, blank=True, verbose_name="邮箱") class Meta: verbose_name = "用户" verbose_name_plural = verbose_name def __str__(self): return self.name class VerifyCode(models.Model): """ 短信验证码 """ code = models.CharField(max_length=10, verbose_name="验证码") mobile = models.CharField(max_length=11, verbose_name="电话") add_time = models.DateTimeField(default=datetime.now, verbose_name="添加时间") class Meta: verbose_name = "短信验证码" verbose_name_plural = verbose_name def __str__(self): return self.code
3-3 Goods Model设计
3.3.1 goods Model的设计;
goods/models.py;
from datetime import datetime from django.db import models from DjangoUeditor.models import UEditorField # Create your models here. class GoodsCategory(models.Model): """ 商品类别 """ CATEGORY_TYPE = ( (1, "一级类目"), (2, "二级类目"), (3, "三级类目"), ) name = models.CharField(default="", max_length=30, verbose_name="类别名", help_text="类别名") code = models.CharField(default="", max_length=30, verbose_name="类别code", help_text="类别code") desc = models.TextField(default="", verbose_name="类别描述", help_text="类别描述") category_type = models.IntegerField(choices=CATEGORY_TYPE, verbose_name="类目级别", help_text="类目级别") parent_category = models.ForeignKey("self", null=True, blank=True, verbose_name="父类别", related_name="sub_cat") is_tab = models.BooleanField(default=False, verbose_name="是否导航", help_text="是否导航") add_time = models.DateTimeField(default=datetime.now, verbose_name="添加时间") class Meta: verbose_name = "商品类别" verbose_name_plural = verbose_name def __str__(self): return self.name class GoodesCategoryBrand(models.Model): """ 品牌名 """ name = models.CharField(default="", max_length=30, verbose_name="品牌名", help_text="品牌名") desc = models.TextField(default="", max_length=200, verbose_name="品牌描述", help_text="品牌描述") image = models.ImageField(max_length=200, upload_to="brand/images/") add_time = models.DateTimeField(default=datetime.now, verbose_name="添加时间") class Meta: verbose_name = "品牌" verbose_name_plural = verbose_name def __str__(self): return self.name class Goods(models.Model): """ 商品 """ category = models.ForeignKey(GoodsCategory, verbose_name="商品类目") goods_sn = models.CharField(max_length=50, default="", verbose_name="商品唯一货号") name = models.CharField(max_length=300, default="", verbose_name="商品名") click_name = models.IntegerField(default=0, verbose_name="点击数") sold_num = models.IntegerField(default=0, verbose_name="商品销售数量") fav_num = models.IntegerField(default=0, verbose_name="收藏数") goods_num = models.IntegerField(default=0, verbose_name="库存数") market_price = models.FloatField(default=0, verbose_name="市场价格") shop_price = models.FloatField(default=0, verbose_name="本店价格") goods_brief = models.TextField(max_length=500, verbose_name="商品简短描述") goods_desc = UEditorField(verbose_name=u"内容", imagePath="goods/images/", width=1000, height=300, filePath="goods/files/", default='') ship_free = models.BooleanField(default=True, verbose_name="是否承担运费") goods_front_image = models.ImageField(upload_to="", null=True, blank=True, verbose_name="商品封面图") is_new = models.BooleanField(default=False, verbose_name="是否新品") is_hot = models.BooleanField(default=False, verbose_name="是否热销") add_time = models.DateTimeField(default=datetime.now, verbose_name="添加时间") class Meta: verbose_name = "商品" verbose_name_plural = verbose_name def __str__(self): return self.name class GoodsImage(models.Model): """ 商品轮播图 """ goods = models.ForeignKey(Goods, verbose_name="商品", related_name="images") image = models.ImageField(upload_to="", verbose_name="图片", null=True, blank=True) image_url = models.CharField(max_length=300, null=True, blank=True, verbose_name="图片URL") add_time = models.DateTimeField(default=datetime.now, verbose_name="添加时间") class Meta: verbose_name = "商品轮播图" verbose_name_plural = verbose_name def __str__(self): return self.goods.name class Banner(models.Model): """ 轮播的商品 """ goods = models.ForeignKey(Goods, verbose_name="商品") image = models.ImageField(upload_to="banner", verbose_name="轮播图片") index = models.IntegerField(default=0, verbose_name="轮播顺序") add_time = models.DateTimeField(default=datetime.now, verbose_name="添加时间") class Meta: verbose_name = "轮播商品" verbose_name_plural = verbose_name def __str__(self): return self.goods.name
3-4 Trade交易的Model设计
3.4.1 购物车和订单的概念;
trade/models.py设计;
from datetime import datetime from django.db import models from django.contrib.auth import get_user_model from goods.models import Goods User = get_user_model() # Create your models here. class ShoppingCart(models.Model): """ 购物车 """ user = models.ForeignKey(User, verbose_name=u"用户") goods = models.ForeignKey(Goods, verbose_name=u"商品") nums = models.IntegerField(default=0, verbose_name=u"购买数量") add_time = models.DateTimeField(default=datetime.now, verbose_name=u"添加时间") class Meta: verbose_name = "购物车" verbose_name_plural = verbose_name def __str__(self): return "%s(%d)".format(self.goods.name, self.goods_num) class OrderInfo(models.Model): """ 订单 """ ORDER_STATUS = ( ("sucess", "成功"), ("cancel", "取消"), ("cancel", "待支付"), ) # PAY_TYPE = ( # ("alipay", "支付宝"), # ("wechat", "微信"), # ) user = models.ForeignKey(User, verbose_name=u"用户") order_sn = models.CharField(max_length=30, unique=True, verbose_name="订单号") trade_no = models.CharField(max_length=50, unique=True, null=True, blank=True, verbose_name=u"交易号") pay_status = models.CharField(choices=ORDER_STATUS, max_length=10, verbose_name="订单状态") post_script = models.CharField(max_length=200, verbose_name="订单留言") order_mount = models.FloatField(default=0.0, verbose_name="订单金额") pay_time = models.DateTimeField(null=True, blank=True, verbose_name="支付时间") # 用户信息 address = models.CharField(max_length=100, default="", verbose_name="收货地址") signer_name = models.CharField(max_length=20, default="", verbose_name="签收人") signer_mobile = models.CharField(max_length=11, verbose_name="联系电话") add_time = models.DateTimeField(default=datetime.now, verbose_name="添加时间") class Meta: verbose_name = "订单" verbose_name_plural = verbose_name def __str__(self): return str(self.order_sn) class OrderGoods(models.Model): """ 订单的商品详情 """ order = models.ForeignKey(OrderInfo, verbose_name="订单信息") goods = models.ForeignKey(Goods, verbose_name="商品") goods_num = models.IntegerField(default=0, verbose_name="商品数量") add_time = models.DateTimeField(default=datetime.now, verbose_name="添加时间") class Meta: verbose_name = "订单商品" verbose_name_plural = verbose_name def __str__(self): return str(self.order.order_sn)
3-5 用户操作的Model设计
3.5.1 user_opration下的models.py设计;
from datetime import datetime from django.db import models from django.contrib.auth import get_user_model from goods.models import Goods User = get_user_model() # Create your models here. class UserFav(models.Model): """ 用户收藏 """ user = models.ForeignKey(User, verbose_name="用户") goods = models.ForeignKey(Goods, verbose_name="商品") add_time = models.DateTimeField(default=datetime.now, verbose_name="添加时间") class Meta: verbose_name = "用户收藏" verbose_name_plural = verbose_name def __str__(self): return self.user.name class UserLeavingMessage(models.Model): """ 用户留言 """ MESSAGE_CHOICES = ( (1, "留言"), (2, "投诉"), (3, "询问"), (4, "售后"), (5, "求购"), ) user = models.ForeignKey(User, verbose_name="用户") message_type = models.IntegerField(default=1, choices=MESSAGE_CHOICES, verbose_name="", help_text="留言类型:1(留言),2(投诉),3(询问),4(售后),5(求购)") subject = models.CharField(max_length=100, default="", verbose_name="主题") message = models.TextField(default="", verbose_name="留言内容", help_text="留言内容") file = models.FileField(verbose_name="上传的文件", help_text="上传的文件") add_time = models.DateTimeField(default=datetime.now, verbose_name="添加时间") class Meta: verbose_name = "用户留言" verbose_name_plural = verbose_name def __str__(self): return self.subject class UserAddress(models.Model): """ 用户收货地址 """ user = models.ForeignKey(User, verbose_name="用户") address = models.CharField(max_length=100, default="", verbose_name="区域") signer_name = models.CharField(max_length=100, default="", verbose_name="签收人") signer_mobile = models.CharField(max_length=11, default="", verbose_name="电话") add_time = models.DateTimeField(default=datetime.now, verbose_name="添加时间") class Meta: verbose_name = "收货地址" verbose_name_plural = verbose_name def __str__(self): return self.address
3-6 migrations原理及表生成
3.6.1 settings.py中配置了AUTH_USER_MODEL = 'users.UserProfile'就不会再生成auth_user表;
3.6.2 如果migrate不生效,可删除django_migrations中的部分表;
3.6.3 通过Navvicat 与migrations不建议混用;
3-7 xadmin后台管理系统的配置
3.7.1 xadmin的配置settings.py中添加两项:xadmin、crispy_forms并在各个app中添加xadmin.py文件;
3.7.2 安装xadmin的依赖包文件;
地址如下:https://github.com/sshwsfc/xadmin/blob/master/requirements.txt
- pip3 install xlwt xlsxwriter
3.7.3 改为中文显示;
LANGUAGE_CODE = 'zh-hans' TIME_ZONE = 'Asia/Shanghai' USE_I18N = True USE_L10N = True USE_TZ = False
3-8 导入商品类别数据
3.8.1 category_data.py
#!/usr/bin/env python # encoding: utf-8 row_data = [ { 'sub_categorys': [ { 'sub_categorys': [ { 'code': 'yr', 'name': '羊肉' }, { 'code': 'ql', 'name': '禽类' }, { 'code': 'zr', 'name': '猪肉' }, { 'code': 'nr', 'name': '牛肉' } ], 'code': 'jprl', 'name': '精品肉类' }, { 'sub_categorys': [ { 'code': 'cb', 'name': '参鲍' }, { 'code': 'yu', 'name': '鱼' }, { 'code': 'xia', 'name': '虾' }, { 'code': 'xb', 'name': '蟹/贝' } ], 'code': 'hxsc', 'name': '海鲜水产' }, { 'sub_categorys': [ { 'code': 'xhd_xyd', 'name': '松花蛋/咸鸭蛋' }, { 'code': 'jd', 'name': '鸡蛋' } ], 'code': 'dzp', 'name': '蛋制品' }, { 'sub_categorys': [ { 'code': 'sc', 'name': '生菜' }, { 'code': 'bc', 'name': '菠菜' }, { 'code': 'yj', 'name': '圆椒' }, { 'code': 'xlh', 'name': '西兰花' } ], 'code': 'ycl', 'name': '叶菜类' }, { 'sub_categorys': [ ], 'code': 'gjl', 'name': '根茎类' }, { 'sub_categorys': [ ], 'code': 'qgl', 'name': '茄果类' }, { 'sub_categorys': [ ], 'code': 'jgl', 'name': '菌菇类' }, { 'sub_categorys': [ ], 'code': 'jksx', 'name': '进口生鲜' } ], 'code': 'sxsp', 'name': '生鲜食品' }, { 'sub_categorys': [ { 'sub_categorys': [ { 'code': 'wly', 'name': '五粮液' }, { 'code': 'lzlj', 'name': '泸州老窖' }, { 'code': 'mt', 'name': '茅台' } ], 'code': 'bk', 'name': '白酒' }, { 'sub_categorys': [ ], 'code': 'ptj', 'name': '葡萄酒' }, { 'sub_categorys': [ ], 'code': 'yj', 'name': '洋酒' }, { 'sub_categorys': [ ], 'code': 'pj', 'name': '啤酒' }, { 'sub_categorys': [ { 'code': 'qtpp', 'name': '其他品牌' }, { 'code': 'hj', 'name': '黄酒' }, { 'code': 'ysj', 'name': '养生酒' } ], 'code': 'qtjp', 'name': '其他酒品' }, { 'sub_categorys': [ ], 'code': 'yls', 'name': '饮料/水' }, { 'sub_categorys': [ { 'code': 'bld', 'name': '白兰地' }, { 'code': 'wsj', 'name': '威士忌' } ], 'code': 'hj', 'name': '红酒' } ], 'code': 'jsyl', 'name': '酒水饮料' }, { 'sub_categorys': [ { 'sub_categorys': [ { 'code': '其他食用油', 'name': '其他食用油' }, { 'code': '菜仔油', 'name': '菜仔油' }, { 'code': '花生油', 'name': '花生油' }, { 'code': '橄榄油', 'name': '橄榄油' }, { 'code': '礼盒', 'name': '礼盒' } ], 'code': '食用油', 'name': '食用油' }, { 'sub_categorys': [ { 'code': '面粉/面条', 'name': '面粉/面条' }, { 'code': '大米', 'name': '大米' }, { 'code': '意大利面', 'name': '意大利面' } ], 'code': '米面杂粮', 'name': '米面杂粮' }, { 'sub_categorys': [ { 'code': '调味油/汁', 'name': '调味油/汁' }, { 'code': '酱油/醋', 'name': '酱油/醋' } ], 'code': '厨房调料', 'name': '厨房调料' }, { 'sub_categorys': [ ], 'code': '南北干货', 'name': '南北干货' }, { 'sub_categorys': [ ], 'code': '方便速食', 'name': '方便速食' }, { 'sub_categorys': [ ], 'code': '调味品', 'name': '调味品' } ], 'code': '粮油副食', 'name': '粮油副食' }, { 'sub_categorys': [ { 'sub_categorys': [ { 'code': '西红柿', 'name': '西红柿' }, { 'code': '韭菜', 'name': '韭菜' }, { 'code': '青菜', 'name': '青菜' } ], 'code': '有机蔬菜', 'name': '有机蔬菜' }, { 'sub_categorys': [ { 'code': '甘蓝', 'name': '甘蓝' }, { 'code': '胡萝卜', 'name': '胡萝卜' }, { 'code': '黄瓜', 'name': '黄瓜' } ], 'code': '精选蔬菜', 'name': '精选蔬菜' }, { 'sub_categorys': [ { 'code': '火龙果', 'name': '火龙果' }, { 'code': '菠萝蜜', 'name': '菠萝蜜' }, { 'code': '奇异果', 'name': '奇异果' } ], 'code': '进口水果', 'name': '进口水果' }, { 'sub_categorys': [ { 'code': '水果礼盒', 'name': '水果礼盒' }, { 'code': '苹果', 'name': '苹果' }, { 'code': '雪梨', 'name': '雪梨' } ], 'code': '国产水果', 'name': '国产水果' } ], 'code': '蔬菜水果', 'name': '蔬菜水果' }, { 'sub_categorys': [ { 'sub_categorys': [ { 'code': '果冻', 'name': '果冻' }, { 'code': '枣类', 'name': '枣类' }, { 'code': '蜜饯', 'name': '蜜饯' }, { 'code': '肉类零食', 'name': '肉类零食' }, { 'code': '坚果炒货', 'name': '坚果炒货' } ], 'code': '休闲零食', 'name': '休闲零食' }, { 'sub_categorys': [ { 'code': '创意喜糖', 'name': '创意喜糖' }, { 'code': '口香糖', 'name': '口香糖' }, { 'code': '软糖', 'name': '软糖' }, { 'code': '棒棒糖', 'name': '棒棒糖' } ], 'code': '糖果', 'name': '糖果' }, { 'sub_categorys': [ { 'code': '夹心巧克力', 'name': '夹心巧克力' }, { 'code': '白巧克力', 'name': '白巧克力' }, { 'code': '松露巧克力', 'name': '松露巧克力' }, { 'code': '黑巧克力', 'name': '黑巧克力' } ], 'code': '巧克力', 'name': '巧克力' }, { 'sub_categorys': [ { 'code': '牛肉干', 'name': '牛肉干' }, { 'code': '猪肉脯', 'name': '猪肉脯' }, { 'code': '牛肉粒', 'name': '牛肉粒' }, { 'code': '猪肉干', 'name': '猪肉干' } ], 'code': '肉干肉脯/豆干', 'name': '肉干肉脯/豆干' }, { 'sub_categorys': [ { 'code': '鱿鱼足', 'name': '鱿鱼足' }, { 'code': '鱿鱼丝', 'name': '鱿鱼丝' }, { 'code': '墨鱼/乌贼', 'name': '墨鱼/乌贼' }, { 'code': '鱿鱼仔', 'name': '鱿鱼仔' }, { 'code': '鱿鱼片', 'name': '鱿鱼片' } ], 'code': '鱿鱼丝/鱼干', 'name': '鱿鱼丝/鱼干' } ], 'code': '休闲食品', 'name': '休闲食品' }, { 'sub_categorys': [ { 'sub_categorys': [ ], 'code': '进口奶品', 'name': '进口奶品' }, { 'sub_categorys': [ ], 'code': '国产奶品', 'name': '国产奶品' }, { 'sub_categorys': [ ], 'code': '奶粉', 'name': '奶粉' }, { 'sub_categorys': [ ], 'code': '有机奶', 'name': '有机奶' }, { 'sub_categorys': [ ], 'code': '原料奶', 'name': '原料奶' } ], 'code': '奶类食品', 'name': '奶类食品' }, { 'sub_categorys': [ { 'sub_categorys': [ ], 'code': '菌菇类', 'name': '菌菇类' }, { 'sub_categorys': [ ], 'code': '腌干海产', 'name': '腌干海产' }, { 'sub_categorys': [ ], 'code': '汤料', 'name': '汤料' }, { 'sub_categorys': [ ], 'code': '豆类', 'name': '豆类' }, { 'sub_categorys': [ ], 'code': '干菜/菜干', 'name': '干菜/菜干' }, { 'sub_categorys': [ ], 'code': '干果/果干', 'name': '干果/果干' }, { 'sub_categorys': [ ], 'code': '豆制品', 'name': '豆制品' }, { 'sub_categorys': [ ], 'code': '腊味', 'name': '腊味' } ], 'code': '天然干货', 'name': '天然干货' }, { 'sub_categorys': [ { 'sub_categorys': [ ], 'code': '白茶', 'name': '白茶' }, { 'sub_categorys': [ ], 'code': '红茶', 'name': '红茶' }, { 'sub_categorys': [ ], 'code': '绿茶', 'name': '绿茶' } ], 'code': '精选茗茶', 'name': '精选茗茶' } ]
3.8.2 import_category_data.py
# -*- coding: utf-8 -*- __author__ = 'bobby' #独立使用django的model import sys import os pwd = os.path.dirname(os.path.realpath(__file__)) sys.path.append(pwd+"../") os.environ.setdefault("DJANGO_SETTINGS_MODULE", "MxShop.settings") import django django.setup() from goods.models import GoodsCategory from db_tools.data.category_data import row_data for lev1_cat in row_data: lev1_intance = GoodsCategory() lev1_intance.code = lev1_cat["code"] lev1_intance.name = lev1_cat["name"] lev1_intance.category_type = 1 lev1_intance.save() for lev2_cat in lev1_cat["sub_categorys"]: lev2_intance = GoodsCategory() lev2_intance.code = lev2_cat["code"] lev2_intance.name = lev2_cat["name"] lev2_intance.category_type = 2 lev2_intance.parent_category = lev1_intance lev2_intance.save() for lev3_cat in lev2_cat["sub_categorys"]: lev3_intance = GoodsCategory() lev3_intance.code = lev3_cat["code"] lev3_intance.name = lev3_cat["name"] lev3_intance.category_type = 3 lev3_intance.parent_category = lev2_intance lev3_intance.save()
3-9 导入商品和商品类别数据
3.9.1 product_data.py
#!/usr/bin/env python # encoding: utf-8 row_data = [ { 'images': [ 'goods/images/1_P_1449024889889.jpg', 'goods/images/1_P_1449024889264.jpg', 'goods/images/1_P_1449024889726.jpg', 'goods/images/1_P_1449024889018.jpg', 'goods/images/1_P_1449024889287.jpg' ], 'categorys': [ '首页', '生鲜食品', '根茎类' ], 'market_price': '¥232元', 'name': '新鲜水果甜蜜香脆单果约800克', 'desc': '食用百香果可以增加胃部饱腹感,减少余热量的摄入,还可以吸附胆固醇和胆汁之类有机分子,抑制人体对脂肪的吸收。因此,长期食用有利于改善人体营养吸收结构,降低体内脂肪,塑造健康优美体态。', 'sale_price': '¥156元', 'goods_desc':'' }, { 'images': [ 'goods/images/2_P_1448945810202.jpg', 'goods/images/2_P_1448945810814.jpg' ], 'categorys': [ '首页', '生鲜食品', '海鲜水产' ], 'market_price': '¥106元', 'name': '田然牛肉大黄瓜条生鲜牛肉冷冻真空黄牛', 'desc': '前腿+后腿+羊排共8斤,原生态大山放牧羊羔,曾经的皇室贡品,央视推荐,2005年北京招待全球财金首脑。五层专用包装箱+真空包装+冰袋+保鲜箱+顺丰冷链发货,路途保质期8天', 'sale_price': '¥88元', 'goods_desc':'' }, { 'images': [ 'goods/images/7_P_1448945104883.jpg', 'goods/images/7_P_1448945104734.jpg' ], 'categorys': [ '首页', '生鲜食品', '叶菜类' ], 'market_price': '¥286元', 'name': '酣畅家庭菲力牛排10片澳洲生鲜牛肉团购套餐', 'desc': None, 'sale_price': '¥238元', 'goods_desc':'' }, { 'images': [ 'goods/images/47_P_1448946213263.jpg', 'goods/images/47_P_1448946213157.jpg' ], 'categorys': [ '首页', '生鲜食品', '根茎类' ], 'market_price': '¥156元', 'name': '日本蒜蓉粉丝扇贝270克6只装', 'desc': None, 'sale_price': '¥108元', 'goods_desc':'' }, { 'images': [ 'goods/images/10_P_1448944572085.jpg', 'goods/images/10_P_1448944572532.jpg', 'goods/images/10_P_1448944572872.jpg' ], 'categorys': [ '首页', '生鲜食品', '海鲜水产' ], 'market_price': '¥106元', 'name': '内蒙新鲜牛肉1斤清真生鲜牛肉火锅食材', 'desc': None, 'sale_price': '¥88元', 'goods_desc':'' }, { 'images': [ 'goods/images/4_P_1448945381985.jpg', 'goods/images/4_P_1448945381013.jpg' ], 'categorys': [ '首页', '生鲜食品', '茄果类' ], 'market_price': '¥90元', 'name': '乌拉圭进口牛肉卷特级肥牛卷', 'desc': None, 'sale_price': '¥75元', 'goods_desc':'' }, { 'images': [ 'goods/images/8_P_1448945032810.jpg', 'goods/images/8_P_1448945032646.jpg' ], 'categorys': [ '首页', '生鲜食品', '进口生鲜' ], 'market_price': '¥150元', 'name': '五星眼肉牛排套餐8片装原味原切生鲜牛肉', 'desc': None, 'sale_price': '¥125元', 'goods_desc':'' }, { 'images': [ 'goods/images/11_P_1448944388277.jpg', 'goods/images/11_P_1448944388034.jpg', 'goods/images/11_P_1448944388201.jpg' ], 'categorys': [ '首页', '生鲜食品', '海鲜水产' ], 'market_price': '¥31元', 'name': '澳洲进口120天谷饲牛仔骨4份原味生鲜', 'desc': None, 'sale_price': '¥26元', 'goods_desc':'' }, { 'images': [ 'goods/images/6_P_1448945167279.jpg', 'goods/images/6_P_1448945167015.jpg' ], 'categorys': [ '首页', '生鲜食品', '菌菇类' ], 'market_price': '¥239元', 'name': '潮香村澳洲进口牛排家庭团购套餐20片', 'desc': None, 'sale_price': '¥199元', 'goods_desc':'' }, { 'images': [ 'goods/images/9_P_1448944791617.jpg', 'goods/images/9_P_1448944791129.jpg', 'goods/images/9_P_1448944791077.jpg', 'goods/images/9_P_1448944791229.jpg' ], 'categorys': [ '首页', '生鲜食品', '根茎类' ], 'market_price': '¥202元', 'name': '爱食派内蒙古呼伦贝尔冷冻生鲜牛腱子肉1000g', 'desc': None, 'sale_price': '¥168元', 'goods_desc':'' }, { 'images': [ 'goods/images/3_P_1448945490837.jpg', 'goods/images/3_P_1448945490084.jpg' ], 'categorys': [ '首页', '生鲜食品', '精品肉类' ], 'market_price': '¥306元', 'name': '澳洲进口牛尾巴300g新鲜肥牛肉', 'desc': '新鲜羊羔肉整只共15斤,原生态大山放牧羊羔,曾经的皇室贡品,央视推荐,2005年北京招待全球财金首脑。五层专用包装箱+真空包装+冰袋+保鲜箱+顺丰冷链发货,路途保质期8天', 'sale_price': '¥255元', 'goods_desc':'' }, { 'images': [ 'goods/images/48_P_1448943988970.jpg', 'goods/images/48_P_1448943988898.jpg', 'goods/images/48_P_1448943988439.jpg' ], 'categorys': [ '首页', '生鲜食品', '海鲜水产' ], 'market_price': '¥126元', 'name': '新疆巴尔鲁克生鲜牛排眼肉牛扒1200g', 'desc': None, 'sale_price': '¥88元', 'goods_desc':'' }, { 'images': [ 'goods/images/5_P_1448945270390.jpg', 'goods/images/5_P_1448945270067.jpg', 'goods/images/5_P_1448945270432.jpg' ], 'categorys': [ '首页', '生鲜食品', '蛋制品' ], 'market_price': '¥144元', 'name': '澳洲进口安格斯牛切片上脑牛排1000g', 'desc': '澳大利亚是国际公认的没有疯牛病和口蹄疫的国家。为了保持澳大利亚产品的高标准,澳大利亚牛肉业和各级政府共同努力简历了严格的标准和体系,以保证生产的整体化和产品的可追溯性', 'sale_price': '¥120元', 'goods_desc':'' }, { 'images': [ 'images/201705/goods_img/53_P_1495068879687.jpg' ], 'categorys': [ '首页', '生鲜食品', '茄果类' ], 'market_price': '¥120元', 'name': '帐篷出租', 'desc': None, 'sale_price': '¥100元', 'goods_desc':'' }, { 'images': [ 'goods/images/16_P_1448947194687.jpg' ], 'categorys': [ '首页', '酒水饮料', '红酒' ], 'market_price': '¥23元', 'name': '52度茅台集团国隆双喜酒500mlx6', 'desc': '贵州茅台酒厂(集团)保健酒业有限公司生产,是以“龙”字打头的酒水。中国龙文化上下8000年,源远而流长,龙的形象是一种符号、一种意绪、一种血肉相联的情感。', 'sale_price': '¥19元', 'goods_desc':'' }, { 'images': [ 'goods/images/14_P_1448947354031.jpg', 'goods/images/14_P_1448947354433.jpg' ], 'categorys': [ '首页', '酒水饮料', '饮料/水' ], 'market_price': '¥43元', 'name': '52度水井坊臻酿八號500ml', 'desc': None, 'sale_price': '¥36元', 'goods_desc':'' }, { 'images': [ 'goods/images/12_P_1448947547989.jpg' ], 'categorys': [ '首页', '酒水饮料', '其他酒品' ], 'market_price': '¥190元', 'name': '53度茅台仁酒500ml', 'desc': None, 'sale_price': '¥158元', 'goods_desc':'' }, { 'images': [ 'goods/images/46_P_1448946598711.jpg', 'goods/images/46_P_1448946598301.jpg' ], 'categorys': [ '首页', '酒水饮料', '葡萄酒' ], 'market_price': '¥38元', 'name': '双响炮洋酒JimBeamwhiskey美国白占边', 'desc': None, 'sale_price': '¥28元', 'goods_desc':'' }, { 'images': [ 'goods/images/21_P_1448946793276.jpg', 'goods/images/21_P_1448946793153.jpg' ], 'categorys': [ '首页', '酒水饮料', '饮料/水' ], 'market_price': '¥55元', 'name': '西夫拉姆进口洋酒小酒版', 'desc': None, 'sale_price': '¥46元', 'goods_desc':'' }, { 'images': [ 'goods/images/15_P_1448947257324.jpg', 'goods/images/15_P_1448947257580.jpg' ], 'categorys': [ '首页', '酒水饮料', '洋酒' ], 'market_price': '¥22元', 'name': '茅台53度飞天茅台500ml', 'desc': None, 'sale_price': '¥18元', 'goods_desc':'' }, { 'images': [ 'goods/images/13_P_1448947460386.jpg', 'goods/images/13_P_1448947460276.jpg', 'goods/images/13_P_1448947460353.jpg' ], 'categorys': [ '首页', '酒水饮料', '葡萄酒' ], 'market_price': '¥42元', 'name': '52度兰陵·紫气东来1600mL山东名酒', 'desc': None, 'sale_price': '¥35元', 'goods_desc':'' }, { 'images': [ 'goods/images/50_P_1448946543091.jpg', 'goods/images/50_P_1448946542182.jpg' ], 'categorys': [ '首页', '酒水饮料', '饮料/水' ], 'market_price': '¥24元', 'name': 'JohnnieWalker尊尼获加黑牌威士忌', 'desc': None, 'sale_price': '¥20元', 'goods_desc':'' }, { 'images': [ 'goods/images/51_P_1448946466595.jpg', 'goods/images/51_P_1448946466208.jpg' ], 'categorys': [ '首页', '酒水饮料', '洋酒' ], 'market_price': '¥31元', 'name': '人头马CLUB特优香槟干邑350ml', 'desc': None, 'sale_price': '¥26元', 'goods_desc':'' }, { 'images': [ 'goods/images/17_P_1448947102246.jpg' ], 'categorys': [ '首页', '酒水饮料', '啤酒' ], 'market_price': '¥54元', 'name': '张裕干红葡萄酒750ml*6支', 'desc': None, 'sale_price': '¥45元', 'goods_desc':'' }, { 'images': [ 'goods/images/20_P_1448946850602.jpg' ], 'categorys': [ '首页', '酒水饮料', '葡萄酒' ], 'market_price': '¥46元', 'name': '原瓶原装进口洋酒烈酒法国云鹿XO白兰地', 'desc': None, 'sale_price': '¥38元', 'goods_desc':'' }, { 'images': [ 'goods/images/19_P_1448946951581.jpg', 'goods/images/19_P_1448946951726.jpg' ], 'categorys': [ '首页', '酒水饮料', '白酒' ], 'market_price': '¥82元', 'name': '法国原装进口圣贝克干红葡萄酒750ml', 'desc': None, 'sale_price': '¥68元', 'goods_desc':'' }, { 'images': [ 'goods/images/18_P_1448947011435.jpg' ], 'categorys': [ '首页', '酒水饮料', '白酒' ], 'market_price': '¥67元', 'name': '法国百利威干红葡萄酒AOP级6支装', 'desc': None, 'sale_price': '¥56元', 'goods_desc':'' }, { 'images': [ 'goods/images/22_P_1448946729629.jpg' ], 'categorys': [ '首页', '酒水饮料', '洋酒' ], 'market_price': '¥71元', 'name': '芝华士12年苏格兰威士忌700ml', 'desc': None, 'sale_price': '¥59元', 'goods_desc':'' }, { 'images': [ 'goods/images/45_P_1448946661303.jpg' ], 'categorys': [ '首页', '酒水饮料', '饮料/水' ], 'market_price': '¥31元', 'name': '深蓝伏特加巴维兰利口酒送预调酒', 'desc': None, 'sale_price': '¥18元', 'goods_desc':'' }, { 'images': [ 'goods/images/32_P_1448948525620.jpg' ], 'categorys': [ '首页', '蔬菜水果', '精选蔬菜' ], 'market_price': '¥43元', 'name': '赣南脐橙特级果10斤装', 'desc': None, 'sale_price': '¥36元', 'goods_desc':'' }, { 'images': [ 'goods/images/30_P_1448948663450.jpg', 'goods/images/30_P_1448948662571.jpg', 'goods/images/30_P_1448948663221.jpg' ], 'categorys': [ '首页', '蔬菜水果', '进口水果' ], 'market_price': '¥11元', 'name': '泰国菠萝蜜16-18斤1个装', 'desc': '【懒人吃法】菠萝蜜果肉,冰袋保鲜,收货就吃,冰爽Q脆甜,2斤装,全国顺丰空运包邮,发出后48小时内可达,一线城市基本隔天可达', 'sale_price': '¥9元', 'goods_desc':'' }, { 'images': [ 'goods/images/31_P_1448948598947.jpg', 'goods/images/31_P_1448948598475.jpg' ], 'categorys': [ '首页', '蔬菜水果', '国产水果' ], 'market_price': '¥22元', 'name': '四川双流草莓新鲜水果礼盒2盒', 'desc': None, 'sale_price': '¥18元', 'goods_desc':'' }, { 'images': [ 'goods/images/35_P_1448948333610.jpg', 'goods/images/35_P_1448948333313.jpg' ], 'categorys': [ '首页', '蔬菜水果', '有机蔬菜' ], 'market_price': '¥67元', 'name': '新鲜头茬非洲冰草冰菜', 'desc': None, 'sale_price': '¥56元', 'goods_desc':'' }, { 'images': [ 'goods/images/36_P_1448948234405.jpg', 'goods/images/36_P_1448948234250.jpg' ], 'categorys': [ '首页', '蔬菜水果', '有机蔬菜' ], 'market_price': '¥6元', 'name': '仿真蔬菜水果果蔬菜模型', 'desc': None, 'sale_price': '¥5元', 'goods_desc':'' }, { 'images': [ 'goods/images/33_P_1448948479966.jpg', 'goods/images/33_P_1448948479886.jpg' ], 'categorys': [ '首页', '蔬菜水果', '精选蔬菜' ], 'market_price': '¥28元', 'name': '现摘芭乐番石榴台湾珍珠芭乐', 'desc': '''海南产精品释迦果, 释迦是水果中的贵族, 产量少, 味道很甜, 奶香十足, 非常可口, 果裹果园顺丰空运, 保证新鲜.果子个大, 一斤1-2个左右, 大个头的果子更尽兴! ''', 'sale_price': '¥23元', 'goods_desc':'' }, { 'images': [ 'goods/images/34_P_1448948399009.jpg' ], 'categorys': [ '首页', '蔬菜水果', '国产水果' ], 'market_price': '¥46元', 'name': '潍坊萝卜5斤/箱礼盒', 'desc': '脐橙规格是65-90MM左右(标准果果径平均70MM左右,精品果果径平均80MM左右),一斤大概有2-4个左右,脐橙产自江西省赣州市信丰县安西镇,全过程都是采用农家有机肥种植,生态天然', 'sale_price': '¥38元', 'goods_desc':'' }, { 'images': [ 'goods/images/43_P_1448948762645.jpg' ], 'categorys': [ '首页', '休闲食品' ], 'market_price': '¥154元', 'name': '休闲零食膨化食品焦糖/奶油/椒麻味', 'desc': None, 'sale_price': '¥99元', 'goods_desc':'' }, { 'images': [ 'goods/images/38_P_1448949220255.jpg' ], 'categorys': [ '首页', '奶类食品', '奶粉' ], 'market_price': '¥84元', 'name': '蒙牛未来星儿童成长牛奶骨力型190ml*15盒', 'desc': None, 'sale_price': '¥70元', 'goods_desc':'' }, { 'images': [ 'goods/images/44_P_1448948850187.jpg' ], 'categorys': [ '首页', '奶类食品', '进口奶品' ], 'market_price': '¥70元', 'name': '蒙牛特仑苏有机奶250ml×12盒', 'desc': None, 'sale_price': '¥32元', 'goods_desc':'' }, { 'images': [ 'images/201511/goods_img/49_P_1448162819889.jpg' ], 'categorys': [ '首页', '奶类食品' ], 'market_price': '¥1元', 'name': '1元支付测试商品', 'desc': None, 'sale_price': '¥1元', 'goods_desc':'' }, { 'images': [ 'goods/images/40_P_1448949038702.jpg' ], 'categorys': [ '首页', '奶类食品', '进口奶品' ], 'market_price': '¥70元', 'name': '德运全脂新鲜纯牛奶1L*10盒装整箱', 'desc': None, 'sale_price': '¥58元', 'goods_desc':'' }, { 'images': [ 'goods/images/39_P_1448949115481.jpg' ], 'categorys': [ '首页', '奶类食品', '有机奶' ], 'market_price': '¥38元', 'name': '木糖醇红枣早餐奶即食豆奶粉538g', 'desc': None, 'sale_price': '¥32元', 'goods_desc':'' }, { 'images': [ 'goods/images/41_P_1448948980358.jpg' ], 'categorys': [ '首页', '奶类食品', '原料奶' ], 'market_price': '¥26元', 'name': '高钙液体奶200ml*24盒', 'desc': None, 'sale_price': '¥22元', 'goods_desc':'' }, { 'images': [ 'goods/images/37_P_1448949284365.jpg' ], 'categorys': [ '首页', '奶类食品', '国产奶品' ], 'market_price': '¥720元', 'name': '新西兰进口全脂奶粉900g', 'desc': None, 'sale_price': '¥600元', 'goods_desc':'' }, { 'images': [ 'goods/images/42_P_1448948895193.jpg' ], 'categorys': [ '首页', '奶类食品', '进口奶品' ], 'market_price': '¥43元', 'name': '伊利官方直营全脂营养舒化奶250ml*12盒*2提', 'desc': None, 'sale_price': '¥36元', 'goods_desc':'' }, { 'sale_price': '¥156元', 'images': [ 'goods/images/27_P_1448947771805.jpg' ], 'market_price': '¥187元', 'categorys': [ '首页', '粮油副食', '厨房调料' ], 'desc': None, 'name': '维纳斯橄榄菜籽油5L/桶', 'goods_desc':'' }, { 'sale_price': '¥15元', 'images': [ 'goods/images/23_P_1448948070348.jpg' ], 'market_price': '¥18元', 'categorys': [ '首页', '粮油副食', '食用油' ], 'desc': None, 'name': '糙米450gx3包粮油米面', 'goods_desc':'' }, { 'sale_price': '¥45元', 'images': [ 'goods/images/26_P_1448947825754.jpg' ], 'market_price': '¥54元', 'categorys': [ '首页', '粮油副食', '调味品' ], 'desc': None, 'name': '精炼一级大豆油5L色拉油粮油食用油', 'goods_desc':'' }, { 'sale_price': '¥26元', 'images': [ 'goods/images/28_P_1448947699948.jpg', 'goods/images/28_P_1448947699777.jpg' ], 'market_price': '¥31元', 'categorys': [ '首页', '粮油副食', '南北干货' ], 'desc': None, 'name': '橄榄玉米油5L*2桶', 'goods_desc':'' }, { 'sale_price': '¥9元', 'images': [ 'goods/images/24_P_1448948023823.jpg', 'goods/images/24_P_1448948023977.jpg' ], 'market_price': '¥11元', 'categorys': [ '首页', '粮油副食', '方便速食' ], 'desc': None, 'name': '山西黑米农家黑米4斤', 'goods_desc':'' }, { 'sale_price': '¥12元', 'images': [ 'goods/images/25_P_1448947875346.jpg' ], 'market_price': '¥14元', 'categorys': [ '首页', '粮油副食', '米面杂粮' ], 'desc': None, 'name': '稻园牌稻米油粮油米糠油绿色植物油', 'goods_desc':'' }, { 'sale_price': '¥12元', 'images': [ 'goods/images/29_P_1448947631994.jpg' ], 'market_price': '¥14元', 'categorys': [ '首页', '粮油副食', '食用油' ], 'desc': None, 'name': '融氏纯玉米胚芽油5l桶', 'goods_desc':'' } ] pass
3.9.2 import_goods_data.py
# -*- coding: utf-8 -*- __author__ = 'bobby' import sys import os pwd = os.path.dirname(os.path.realpath(__file__)) sys.path.append(pwd+"../") os.environ.setdefault("DJANGO_SETTINGS_MODULE", "MxShop.settings") import django django.setup() from goods.models import Goods, GoodsCategory, GoodsImage from db_tools.data.product_data import row_data for goods_detail in row_data: goods = Goods() goods.name = goods_detail["name"] goods.market_price = float(int(goods_detail["market_price"].replace("¥", "").replace("元", ""))) goods.shop_price = float(int(goods_detail["sale_price"].replace("¥", "").replace("元", ""))) goods.goods_brief = goods_detail["desc"] if goods_detail["desc"] is not None else "" goods.goods_desc = goods_detail["goods_desc"] if goods_detail["goods_desc"] is not None else "" goods.goods_front_image = goods_detail["images"][0] if goods_detail["images"] else "" category_name = goods_detail["categorys"][-1] category = GoodsCategory.objects.filter(name=category_name) if category: goods.category = category[0] goods.save() for goods_image in goods_detail["images"]: goods_image_instance = GoodsImage() goods_image_instance.image = goods_image goods_image_instance.goods = goods goods_image_instance.save()
4-1 restful API介绍
4.1.1 RESTful API详解;
https://www.runoob.com/w3cnote/restful-architecture.html
4.1.2 前后端分离的优缺点,为什么要进行前后端分离;
- 1、PC、APP、PAD等多端以及Android、IOS适应;
- 2、SPA开发模式流程,SPA,single page即单页面——后端提供API接口;
- 3、前后端开发职责不清楚,模板语言DTL、Jinja2应该由谁来写?
- 4、PHP和Java的模本可以写比较复杂的语法;
- 5、开发效率的问题,前后端岗位互等;
- 6、前端一直配置着后端,能力受限;
- 7、后台开发语言和模板高度耦合,导致开发语言依赖严重;
1、前后端学习门槛增加;
2、数据依赖导致文档重要性增加;
3、前端工作量加大;
4、SEO的难度加大-爬虫拿到的数据将减少;如果不考虑SEO,那么前后端分离将成为必然;
5、后端开发模式迁移成本增加;
4.1.3 RESTful API是目前前后端分离的最佳实践-即约定好的标准;
- 轻量,直接通过http,不需要额外的协议,post、get、put、delete的操作;
- 面向资源,一幕了然,具有自解释性;比如商品、课程;
- 数据描述简单,一般通过json或者xml做数据通信;
https://www.ruanyifeng.com/blog/2011/09/restful.html
https://www.ruanyifeng.com/blog/2014/05/restful_api.html
4-2 Vue的基本概念介绍
4.2.1 概念初识;
- 前端工程化;
- 数据双向绑定——mvvm;
- 组件化开发;
4.2.2 Node.js以及webpack出来之后,带动了js的飞速发展;
vue.js:https://cn.vuejs.org/v2/guide/
4.2.3 Vue开发的几个概念;
- webpack-将所有东西变成一个js文件;
- vue、vuex、vue-router、axios(发送Ajax请求);
- ES6、babel;基于ES6开发,babel是一个将ES6转换为ES5的转换器;
4-3 Vue源码结构介绍
4.3.1 Vue项目目录结构介绍;
5-1 Django的view实现商品列表页
5.1.1 Chrome浏览器添加JsonView插件;
views_base.py;
#!/usr/bin/python3 # -*- coding:utf-8 -*- # Project: MxShop # Software: PyCharm # Time : 2018-10-03 15:55 # File : views_base.py # Author : 天晴天朗 # Email : [email protected] from django.views.generic.base import View # from django.views.generic import ListView from goods.models import Goods class GoodsListView(View): def get(self, request): """ 通过Django的view实现商品列表; :param request: :return: """ json_list = [] goods = Goods.objects.all()[:10] # for good in goods: # json_dict = {} # json_dict["name"] = good.name # json_dict["category"] = good.category.name # json_dict["market_price"] = good.market_price # # json_dict["add_time"] = good.add_time # json_list.append(json_dict) # 方法二: # from django.forms.models import model_to_dict # for good in goods: # json_dict = model_to_dict(good) # json_list.append(json_dict) import json from django.core import serializers json_data = serializers.serialize("json", goods) json_data = json.loads(json_data) from django.http import HttpResponse, JsonResponse return JsonResponse(json_data, safe=False)
5-2 Django的serializer序列化model
5.2.1 Django中serializer序列化model的使用;
#!/usr/bin/python3 # -*- coding:utf-8 -*- # Project: MxShop # Software: PyCharm # Time : 2018-10-03 15:55 # File : views_base.py # Author : 天晴天朗 # Email : [email protected] from django.views.generic.base import View # from django.views.generic import ListView from goods.models import Goods class GoodsListView(View): def get(self, request): """ 通过Django的view实现商品列表; :param request: :return: """ json_list = [] goods = Goods.objects.all()[:10] import json from django.core import serializers json_data = serializers.serialize("json", goods) json_data = json.loads(json_data) from django.http import HttpResponse, JsonResponse return JsonResponse(json_data, safe=False)
5-3 apiview方式实现商品列表页-1
5.3.1 使用drf代替Django REST framework的读法;
-
pip3 install coreapi djangorestframework markdown django-filter django-guardian
5.3.2 为什么使用Django REST framework来代替Django + serializer呢?
5.3.3 一定要在settings.py中配置rest_framework;
5-4 apiview实现商品列表页-2
5.4.1 报错处理TypeError: __str__ returned non-string (type NoneType);
https://blog.csdn.net/WangXJ101010/article/details/78458481
goods/views.py;
from django.shortcuts import render # Create your views here. from .serializers import GoodsSerializer from rest_framework.views import APIView from rest_framework.response import Response from .models import Goods class GoodsListView(APIView): """ List all goods """ def get(self, request, format=None): goods = Goods.objects.all()[:10] goods_serializer = GoodsSerializer(goods, many=True) return Response(goods_serializer.data)
5-5 drf的modelserial实现商品列表页功能
5.5.1 Bug处理将name改为username;
5.5.2 rest_framework中的状态码;
5-6 GenericView方式实现商品列表页和分页功能介绍
5.6.1 rest_framework中的分页功能的实现;
goods/views.py;
from django.shortcuts import render from .serializers import GoodsSerializer from rest_framework.response import Response from rest_framework import mixins from rest_framework import generics from rest_framework.pagination import PageNumberPagination from .models import Goods # Create your views here. class GoodsPagination(PageNumberPagination): page_size = 10 page_size_query_param = 'page_size' page_query_param = "p" max_page_size = 100 class GoodsListView(generics.ListAPIView): """ 商品列表页 """ queryset = Goods.objects.all()[:10] serializer_class = GoodsSerializer pagination_class = GoodsPagination
5-7 viewsets和router完成商品列表项
5.7.1 viewsets和router的使用;
urls.py;
"""MxShop URL Configuration The `urlpatterns` list routes URLs to views. For more information please see: https://docs.djangoproject.com/en/1.11/topics/http/urls/ Examples: Function views 1. Add an import: from my_app import views 2. Add a URL to urlpatterns: url(r'^$', views.home, name='home') Class-based views 1. Add an import: from other_app.views import Home 2. Add a URL to urlpatterns: url(r'^$', Home.as_view(), name='home') Including another URLconf 1. Import the include() function: from django.conf.urls import url, include 2. Add a URL to urlpatterns: url(r'^blog/', include('blog.urls')) """ from django.conf.urls import url, include from django.contrib import admin import xadmin from MxShop.settings import MEDIA_ROOT from django.views.static import serve from rest_framework.documentation import include_docs_urls from rest_framework.routers import DefaultRouter from goods.views import GoodsListViewSet router = DefaultRouter() # 配置goods的url; router.register(r'goods', GoodsListViewSet) urlpatterns = [ url(r'^xadmin/', xadmin.site.urls), url(r'^api-auth/', include('rest_framework.urls', namespace='rest_framework')), # 商品列表页; url(r'^', include(router.urls)), url(r'docs/', include_docs_urls(title="慕学生鲜")), ]
5-8 drf的Apiview、GenericView、Viewset和Router的原理分析
5.8.1 xxxx;
5-9 drf的request和response
5.9.1 drf中的request和response;
5-10 drf的过滤
5.10.1 filter的使用;
5-11 drf的搜索和排序
5.11 drf中添加搜索和排序字段;
goods/views.py;
from django.shortcuts import render from .serializers import GoodsSerializer from rest_framework.views import APIView from rest_framework.response import Response from rest_framework import mixins from rest_framework import generics from rest_framework import filters from rest_framework.pagination import PageNumberPagination from django_filters.rest_framework import DjangoFilterBackend from rest_framework import viewsets from .models import Goods from .filter import GoodsFilter # Create your views here. class GoodsPagination(PageNumberPagination): page_size = 10 page_size_query_param = 'page_size' page_query_param = "p" max_page_size = 100 class GoodsListViewSet(mixins.ListModelMixin, viewsets.GenericViewSet): """ 商品列表页-分页,搜索,过滤,排序; """ queryset = Goods.objects.all() serializer_class = GoodsSerializer pagination_class = GoodsPagination filter_backends = (DjangoFilterBackend,filters.SearchFilter,filters.OrderingFilter) filter_class = GoodsFilter search_fields = ('=name', 'goods_brief', 'goods_desc') ordering_fields = ('sold_num','add_time')
5-12 总结
6-1 商品列表数据接口-1
6-2 商品类别数据接口-2
6.2.1 serializer.py;
#!/usr/bin/python3 # -*- coding:utf-8 -*- # Project: MxShop # Software: PyCharm # Time : 2018-10-03 16:42 # File : serializers.py # Author : 天晴天朗 # Email : [email protected] from rest_framework import serializers from goods.models import Goods, GoodsCategory class CategorySerializer3(serializers.ModelSerializer): class Meta: model = GoodsCategory fields = "__all__" class CategorySerializer2(serializers.ModelSerializer): sub_cat = CategorySerializer3(many=True) class Meta: model = GoodsCategory fields = "__all__" class CategorySerializer(serializers.ModelSerializer): sub_cat = CategorySerializer2(many=True) class Meta: model = GoodsCategory fields = "__all__" class GoodsSerializer(serializers.ModelSerializer): category = CategorySerializer() class Meta: model = Goods fields = "__all__"
6-3 Vue展示商品分类数据
6.3.1 跨域问题的解决;https://github.com/ottoyiu/django-cors-headers
6-4 Vue展示商品列表页数据-1
6-5 Vue展示商品列表页数据-2
6-6 Vue的商品搜索功能
7-1 drf的token登录和原理-1
7.1.1 通过执行数据库迁移操作,生成authtoken_token表;
7-2 drf的token登录和原理-2
7.2.1 Django的请求流程图;
http://www.projectsedu.com/2016/10/17/django%E4%BB%8E%E8%AF%B7%E6%B1%82%E5%88%B0%E8%BF%94%E5%9B%9E%E9%83%BD%E7%BB%8F%E5%8E%86%E4%BA%86%E4%BB%80%E4%B9%88/
7-3 viewsets配置认证类
7.3.1 goods/views.py;
from django.shortcuts import render from rest_framework.views import APIView from rest_framework.response import Response from rest_framework import mixins from rest_framework import generics from rest_framework import filters from rest_framework.pagination import PageNumberPagination from django_filters.rest_framework import DjangoFilterBackend from rest_framework import viewsets from rest_framework.authentication import TokenAuthentication from .models import Goods, GoodsCategory from .filters import GoodsFilter from .serializers import GoodsSerializer, CategorySerializer # Create your views here. class GoodsPagination(PageNumberPagination): page_size = 10 page_size_query_param = 'page_size' page_query_param = "page" max_page_size = 100 class GoodsListViewSet(mixins.ListModelMixin, viewsets.GenericViewSet): """ 商品列表页-分页,搜索,过滤,排序; """ queryset = Goods.objects.all() serializer_class = GoodsSerializer pagination_class = GoodsPagination authentication_classes = (TokenAuthentication,) filter_backends = (DjangoFilterBackend, filters.SearchFilter, filters.OrderingFilter) filter_class = GoodsFilter search_fields = ('=name', 'goods_brief', 'goods_desc') ordering_fields = ('sold_num', 'add_time') class CategoryViewset(mixins.ListModelMixin, mixins.RetrieveModelMixin, viewsets.GenericViewSet): """ list: 商品分类列表数据; """ queryset = GoodsCategory.objects.filter(category_type=1) serializer_class = CategorySerializer
7-4 json web token的原理
7.4.1 JWT-Json Web Token的原理;
https://www.jianshu.com/p/180a870a308a
7-5 json web token方式完成用户认证
7.5.1 djangorestframework-jwt的安装;
官方说明: http://getblimp.github.io/django-rest-framework-jwt/
7-6 Vue和jwt接口调试
7.6.1 jwt在Vue项目中的接口调试;
7-7 云片网发送短信验证码
7.7.1 云片网的短信验证码服务;
云片网:https://www.yunpian.com/
utils/yunpian.py;
#!/usr/bin/python3 # -*- coding:utf-8 -*- # Project: MxShop # Software: PyCharm # Time : 2018-10-04 23:03 # File : yunpian.py # Author : 天晴天朗 # Email : [email protected] import requests class YunPian(object): def __init__(self, api_key): self.api_key = api_key self.single_send_url = "https://sms.yunpian.com/v2/sms/single_send.json" def send_sms(self, code, mobile): parmas = { "apikey": self.api_key, "mobile": mobile, "text": "【崔晓昭】您的验证码是{code}。如非本人操作,请忽略本短信".format(code=code), } response = requests.post(self.single_send_url, data=parmas) import json re_dict = json.loads(response.text) print(re_dict) if __name__ == '__main__': yun_pian = YunPian("13a519fbd6bede962a55e7debdb431bc") yun_pian.send_sms("2017", "13811221893")