视图界面views():
# 地址列表页
def address(request):
# 查收此用户对应的所有地址显示到页面上
buyer_id = request.COOKIES.get('user_id') #获取登录页面的ID
addr_lst = Address.objects.filter(buyer_id=buyer_id) #显示对应ID的所有收货地址
# print(addr_lst)
return render(request, 'buyer/address.html', {'addr_lst': addr_lst}) #使用<for>标签循环套取 (addr_lst)使用
# 添加地址
def add_address(request):
if request.method == 'POST': #如果提交方式是POST
buyer_name = request.POST.get('buyer') #提交的数据
buyer_phone = request.POST.get('buyer_phone')
buyer_address = request.POST.get('buyer_address')
addr = Address() #建立与地址数据的链接
addr.address = buyer_address # 数据对应
addr.phone = buyer_phone
addr.recver = buyer_name
user_id = request.COOKIES.get('user_id')
addr.buyer = Buyer.objects.get(id=int(user_id))# addr.buyer_id= 自动补齐 _id 看modles
addr.save() #存入数据库
return redirect('/buyer/address/') #重定向到地址页面
return render(request, 'buyer/addAddress.html')
# 修改地址
def change_address(request):
if request.method == 'GET': # 删除和修改html提交方式是<a>标签 用的是GET 方式
address_id = request.GET.get('id')
address_obj = Address.objects.get(id=address_id)
return render(request, 'buyer/cha