二手车项目(四)
1.添加汽车到购物车
import datetime from django.core.exceptions import ObjectDoesNotExist from django.http import HttpResponse from django.shortcuts import render, redirect from buy.models import Cart, Orders from sale.models import Carinfo from userinfo.models import Users def add_order(request): ''' 添加到购物车 :param request: :return: ''' # 验证用户是否登录 if request.user.is_authenticated(): car_id = request.GET['carid'] print('car_id-------------', car_id) try: car = Carinfo.objects.get(id=car_id) picture = car.picture # 汽车图片 brand = str(car.serbran) + str(car.ctitle) # 汽车品牌型号 price = car.extractprice # 汽车价格 mileage = car.mileage # 行驶里程 newprice = car.newprice # 新车报价 except ObjectDoesNotExist as e: return HttpResponse(e) return render(request, 'order.html', {'car':locals()}) else: return redirect('/userinfo/login') def del_order(request): ''' 取消订单 :param request: :return: ''' user_id = request.user.id car_id = request.GET['carid'] try: Cart.objects.filter(suser_id=user_id, car_id=car_id).delete() except ObjectDoesNotExist as e: return HttpResponse(e) return redirect('/') def confirm_order(request): ''' 确认订单 :param request: :return: ''' # 已经登录 if request.user.is_authenticated(): car_id = request.GET['carid'] orderNO = datetime.datetime.now().strftime('%Y%m%d%H%M%S') cart = Cart.objects.filter(car_id=car_id)[0] car = Carinfo.objects.filter(id=car_id)[0] picture = cart.picture brand = cart.brand price = cart.price mileage = cart.mileage newprice = cart.newprice sale_user = car.user buy_user = request.user # 创建新的订单对象 Orders.objects.create(sale_user=sale_user, buy_user=buy_user, brand=brand, picture=picture, price=price, newprice=newprice, mileage=mileage, orderno=orderNO) user_id = request.user.id # 查询当前登录用户的所有订单 orders = Orders.objects.filter(buy_user=user_id).order_by('-id') # 查询当前登录用户信息 user = Users.objects.get(id=user_id) # 查询当前登录用户所卖的车 car = Carinfo.objects.filter(user_id=user_id, ispurchase=0, isdelete=0) return render(request, 'user-info.html', {'order':locals()}) else: return redirect('/userinfo/login/')
2..删除订单
def del_order(request): ''' 取消订单 :param request: :return: ''' user_id = request.user.id car_id = request.GET['carid'] try: Cart.objects.filter(suser_id=user_id, car_id=car_id).delete() except ObjectDoesNotExist as e: return HttpResponse(e) return redirect('/')
3.确认订单
def confirm_order(request): ''' 确认订单 :param request: :return: ''' # 已经登录 if request.user.is_authenticated(): car_id = request.GET['carid'] orderNO = datetime.datetime.now().strftime('%Y%m%d%H%M%S') cart = Cart.objects.filter(car_id=car_id)[0] car = Carinfo.objects.filter(id=car_id)[0] picture = cart.picture brand = cart.brand price = cart.price mileage = cart.mileage newprice = cart.newprice sale_user = car.user buy_user = request.user # 创建新的订单对象 Orders.objects.create(sale_user=sale_user, buy_user=buy_user, brand=brand, picture=picture, price=price, newprice=newprice, mileage=mileage, orderno=orderNO) user_id = request.user.id # 查询当前登录用户的所有订单 orders = Orders.objects.filter(buy_user=user_id).order_by('-id') # 查询当前登录用户信息 user = Users.objects.get(id=user_id) # 查询当前登录用户所卖的车 car = Carinfo.objects.filter(user_id=user_id, ispurchase=0, isdelete=0) return render(request, 'user-info.html', {'order':locals()}) else: return redirect('/userinfo/login/')