创建应用(coupons)
![](https://img2020.cnblogs.com/blog/892740/202003/892740-20200321065707940-387652983.png)
把应用添加到settings.py文件中
![](https://img2020.cnblogs.com/blog/892740/202003/892740-20200321065708409-1674076955.png)
创建数据模型
![](https://img2020.cnblogs.com/blog/892740/202003/892740-20200321065708701-2144205367.png)
创建优惠码表单forms.py
![](https://img2020.cnblogs.com/blog/892740/202003/892740-20200321065709020-961379208.png)
添加到admin后台,方便控制
![](https://img2020.cnblogs.com/blog/892740/202003/892740-20200321065709233-1498221876.png)
运行命令
python manage.py makemigration
s 和
python manage.py migrate
![](https://img2020.cnblogs.com/blog/892740/202003/892740-20200321065709435-722805984.png)
在订单模型中添加优惠码逻辑
![](https://img2020.cnblogs.com/blog/892740/202003/892740-20200321065709710-916415837.png)
在订单系统模型中添加优惠逻辑
![](https://img2020.cnblogs.com/blog/892740/202003/892740-20200321065709979-1276892402.png)
在订单系统view视图中添加判断逻辑
![](https://img2020.cnblogs.com/blog/892740/202003/892740-20200321065710300-1271077737.png)
在购物车初始化时从session中获取信息
![](https://img2020.cnblogs.com/blog/892740/202003/892740-20200321065710647-1920538356.png)
![](https://img2020.cnblogs.com/blog/892740/202003/892740-20200321065710959-1851649823.png)
在订单显示模板中添加显示信息
![](https://img2020.cnblogs.com/blog/892740/202003/892740-20200321065711225-1165429723.png)
在优惠券应用视图中创建优惠券
![](https://img2020.cnblogs.com/blog/892740/202003/892740-20200321065711548-1468598259.png)
在订单应用中返回优惠券
![](https://img2020.cnblogs.com/blog/892740/202003/892740-20200321065711830-1299687090.png)
在coupons应用中创建urls.py,添加路由
![](https://img2020.cnblogs.com/blog/892740/202003/892740-20200321065712204-829949120.png)
把coupons的路由导入项目的urls.py文件中
![](https://img2020.cnblogs.com/blog/892740/202003/892740-20200321065712514-1276501961.png)
设置优惠码效
![](https://img2020.cnblogs.com/blog/892740/202003/892740-20200321065712851-1805150966.png)