订单
- 效果图
- 收货地址
- 去结算
- 立即购买
- 我的订单
- 项目步骤
- 逻辑分析
- 建模
- 代码实现
- 添加地址和修改默认地址
- 立即购买
- 逻辑分析
- 代码实现
- 去结算
- 逻辑分析
- 代码实现
- 我的订单
效果图
收货地址
去结算
立即购买
我的订单
项目步骤
逻辑分析
- 现实中购物流程一般是:立即购买(加入购物车)->填写收货信息表->提交订单->支付->我的订单
- 所以这里在点击购买前需要添加收货信息表,所以需要创建一个收货信息表
- 收货信息表应该包含收件人,邮编,电话,地址,是否为默认收货地址
- 因为用户可以自行选择默认地址,所以需要一个路由来改变默认收货地址
- 当立即购买还是去结算购物车都需要生成一个属于当前用户的订单,每个订单则需要展示其所有商品详情
建模
buyer/models.py
....
#地址
class Receiver(models.Model):
"""收件人实体类"""
name = models.CharField(max_length=64) # 姓名
postcode = models.CharField(max_length=64) # 邮编
phone = models.CharField(max_length=64, null=True) # 电话
address = models.CharField(max_length=64, null=True) # 地址
isdefault = models.BooleanField(default=1, verbose_name='默认收货地址') # 是否为默认收获地址
buyer = models.ForeignKey(to=Buyer, on_delete=models.CASCADE, verbose_name='所属买家', null=True) # 所熟买家
# 订单表和订单详情表(一对多)
# 订单表格中-->> 订单编号、总价格、....
# 订单详情表 -->> 商品的详情信息。
# 订单表
class Orders(models.Model):
orderno = models.CharField(max_length=128) # 订单号 20200824142104
orderdate = models.DateTimeField() # 订单日期
totalnum = models.CharField(max_length=32) # 总价
address = models.CharField(max_length=128) # 用户地址,
state = models.BooleanField(default=False) # 订单状态 默认没有支付
buyer = models.ForeignKey(to=Buyer, on_delete=models.CASCADE) # 用户关系
# 订单详情表
class OrderDetail(models.Model):
goodsname = models.CharField(max_length=32) # 商品名称
goodsprice = models.DecimalField(max_digits=5, decimal_places=2) # 商品价格
goodsnum = models.IntegerField() # 数量
image = models.CharField(max_length=32) # 商品图片
goodsxiaoji = models.CharField(max_length=32) # 商品小计
storeid = models.CharField(max_length=32) # 店铺id
orders = models.ForeignKey(to=Orders, on_delete=models.CASCADE) # 订单和订单详情关系
python3 manage.py makemigrations
python3 manage.py migrate
代码实现
添加地址和修改默认地址
templates/buyer/user-center-info.html
buyer/views.py
....
#个人信息
def user_center_info(request):
buyer_id = request.COOKIES.get("buyer_id")
buyer_obj = Buyer.objects.get(id=buyer_id)
address_obj_list = Receiver.objects.filter(buyer_id=buyer_id).all()
return render(request,"buyer/user-center-info.html",locals())
#修改个人信息
def edit_user(request):
buyer_id = request.COOKIES.get("buyer_id")
buyer_obj = Buyer.objects.get(id=buyer_id)
if request.method=="POST":
username = request.POST.get("username")
email = request.POST.get("email")
phone = request.POST.get("phone")
address = request.POST.get("address")
buyer_obj.username=username
buyer_obj.email=email
buyer_obj.phone=phone
buyer_obj.address=address
buyer_obj.save()
return redirect("/usercenter")
return render(request,"buyer/edit-user.html",{"buyer_obj":buyer_obj})
#添加收件人信息
def add_address(request):
buyer_id = request.COOKIES.get("buyer_id")
address_obj_list = Receiver.objects.all()
name = request.POST.get("name")
postcode = request.POST.get("postcode")
phone = request.POST.get("phone")
address = request.POST.get("address")
if address_obj_list:
receiver = Receiver.objects.create(name=name, postcode=postcode, phone=phone, address=address,
isdefault=False, buyer_id=buyer_id)
else:
receiver = Receiver.objects.create(name=name, postcode=postcode, phone=phone, address=address,
buyer_id=buyer_id)
receiver.save()
return JsonResponse({"code":"1"})
#改变默认收货人
def update_address_state(request):
id=request.GET.get("id")
buyer_id = request.COOKIES.get("buyer_id")
buyer_obj = Buyer.objects.get(id=buyer_id)
# 获取当前用户下的所有收货地址
address_obj_list = Receiver.objects.filter(buyer_id=buyer_id).all()
# 遍历所有地址,将用户点击的那个设为默认,其他都设为False
for address_obj in address_obj_list:
if address_obj.id != int(id):
address_obj.isdefault=False
else:
address_obj.isdefault = True
address_obj.save()
return JsonResponse({"code": "1"})
#编辑收件人信息
def edit_address(request):
id=request.GET.get("id")
buyer_id = request.COOKIES.get("buyer_id")
buyer_obj = Buyer.objects.get(id=buyer_id)
address_obj=Receiver.objects.get(id=id)
if request.method=='POST':
name = request.POST.get("name")
postcode = request.POST.get("postcode")
phone = request.POST.get("phone")
address = request.POST.get("address")
address_obj.name=name
address_obj.postcode=postcode
address_obj.phone=phone
address_obj.address=address
address_obj.save()
return redirect("/usercenter")
return render(request,"buyer/edit-address.html",{"address_obj":address_obj})
立即购买
逻辑分析
- 当用户点击立即购买,则后台应该马上添加订单,然后跳转到订单页面
- 问题是如何传该商品的数目给后台
- 这里可以通过js获取number并跳转到后台,也可以用ajax这里作者就偷懒直接用js跳转
代码实现
detail.html
buyer/views.py
#立即购买
def now_buy(request):
id=request.GET.get("id")
num=request.GET.get("number")
goods=Goods.objects.get(id=id)
total=goods.price*int(num)
buyer_id=request.COOKIES.get("buyer_id")
receiver=Receiver.objects.filter(buyer_id=buyer_id).first()
print(receiver)
times = datetime.datetime.now()
# 订单号
orderno = times.strftime("%Y%m%d%H%M%S")
# 订单时间
orderdata = times.strftime("%Y-%m-%d %H:%M:%S")
# 用户地址
if receiver:
address = receiver.address + ' (' + receiver.name + ' 收)' + ' ' + receiver.phone
else:
address = ''
orders_obj = Orders.objects.create(orderno=orderno, orderdate=orderdata, totalnum=total, address=address,
buyer_id=buyer_id)
orders_obj.save()
# 创建订单详情表
orderdetail_obj=OrderDetail.objects.create(goodsname=goods.name,goodsprice=goods.price,goodsnum=num,image=goods.image,goodsxiaoji=total,storeid=goods.store_id,orders_id=orders_obj.id)
orderdetail_obj.save()
return render(request,"buyer/place_order_now.html",locals())
去结算
逻辑分析
- 这里和立即购买差不多,但是去结算是以表单的形式传递给后台,所以就可无需js
- 但是这里后台获取的是select传来的数据所以要以列表的形式去接收传来的 数据
代码实现
# 去结算
def place_order(request):
buyer_id = request.COOKIES.get("buyer_id")
receiver = Receiver.objects.filter(buyer_id=buyer_id, isdefault=True).first()
# 订单总价
total = 0
goodsnumtotal = 0
if request.method=="POST":
# 获取表单所有商品id和数目
shopping_id_list=request.POST.getlist("shoppingids")
shopping_num_list=request.POST.getlist("search")
for shopping_id,shopping_num in zip(shopping_id_list,shopping_num_list):
shoppingcar_obj=ShoppingCar.objects.get(id=shopping_id)
# 将商品的数目和小计进行更新
shoppingcar_obj.goodsnum=shopping_num
price = shoppingcar_obj.goodsprice
shoppingcar_obj.goodsxiaoji = price * int(shoppingcar_obj.goodsnum)
total+=shoppingcar_obj.goodsxiaoji
goodsnumtotal+=int(shoppingcar_obj.goodsnum)
shoppingcar_obj.save()
#生成订单
# 生成订单时间
times=datetime.datetime.now()
# 订单号
orderno=times.strftime("%Y%m%d%H%M%S")
# 订单时间
orderdata=times.strftime("%Y-%m-%d %H:%M:%S")
# 用户地址
if receiver:
address=receiver.address+' ('+receiver.name+' 收)'+' '+receiver.phone
else:
address=''
orders_obj=Orders.objects.create(orderno=orderno,orderdate=orderdata,totalnum=total,address=address,buyer_id=buyer_id)
orders_obj.save()
# 生成详细订单
for shopping_id, shopping_num in zip(shopping_id_list, shopping_num_list):
shoppingcar_obj = ShoppingCar.objects.get(id=shopping_id)
orderdetail=OrderDetail.objects.create(goodsname=shoppingcar_obj.goodsname,goodsprice=shoppingcar_obj.goodsprice,goodsnum=shoppingcar_obj.goodsnum,goodsxiaoji=shoppingcar_obj.goodsxiaoji,image=shoppingcar_obj.image,orders_id=orders_obj.id)
orderdetail.save()
# 删除购物车
shoppingcar_obj.delete()
else:
order_id=request.GET.get("order_id")
orderdetail_list=OrderDetail.objects.filter(orders_id=order_id).all()
for orderdetail in orderdetail_list:
total+=float(orderdetail.goodsxiaoji)
goodsnumtotal+=int(orderdetail.goodsnum)
orders_obj=Orders.objects.filter(id=order_id).first()
return render(request,"buyer/place_order.html",locals())
我的订单
buyer/views.py
.....
# 我的订单
def myorders(request):
buyer_id=request.COOKIES.get("buyer_id")
orders_obj_list=Orders.objects.filter(buyer_id=buyer_id).all()
return render(request,'buyer/myorders.html',locals())
路由urls.py
from django.urls import path
from . import views
urlpatterns = [
path('', views.index), # 首页
path('index', views.index), # 首页
path('login', views.login), # 登录
path('email_login', views.email_login), # 邮箱登录
path('get_yzm_ajax', views.yzm_ajax), # 获取邮箱验证码
path('logout', views.logout), # 登出
path('register', views.register), # 注册
path('detail', views.detail), # 详情
path('list', views.list), # 商品列表
# path('more_list_view',views.more_list_view), #显示更多
path('more_list_view', views.GoodsView.as_view()), # 显示更多视图版
path('add_cart', views.add_cart), # 加入购物车
path('cart', views.cart), # 购物车
path('delete_cart', views.delete_cart), # 删除购物车中的商品
#新加
path('usercenter', views.user_center_info), # 个人信息
path('edit_user', views.edit_user), # 修改个人信息
path('add_address', views.add_address), # 添加收件人地址
path('edit_address', views.edit_address), # 添加收件人地址
path('update_address_state', views.update_address_state), # 改变默认收货人
path('place_order', views.place_order), # 去结算
path('now_buy', views.now_buy), # 立即购买
path('myorders', views.myorders), # 我的订单
]