二手车项目(二)
1.我要买车模块
sale应用中
urls.py
1 # encoding: utf-8 2 __author__ = 'Freshman' 3 4 from django.conf.urls import url, include 5 6 from sale import views 7 8 9 urlpatterns = [ 10 # 汽车品牌列表 11 url(r'^brandlist/$',views.brandlist,name='brandlist'), 12 # 汽车价格列表 13 url(r'^price0_10/$', views.price0_10, name='price0_10'), 14 url(r'^price10_30/$', views.price10_30, name='price10_30'), 15 url(r'^price30_80/$', views.price30_80, name='price30_80'), 16 url(r'^price80_/$', views.price80_, name='price80_'), 17 # 汽车详情 18 url(r'^detail/$', views.detail, name='detail'), 19 20 ]
sale-views.py
1.1汽车品牌分类
def brandlist(request): '''汽车品牌分类视图''' # try 捕获对象不存在异常 try: # 接受主页传递的brand brand = request.GET['brand'] # 查询所有品牌信息列表 brand_list = Brand.objects.all() # 通过品牌名称获取对应的品牌对象 brand = Brand.objects.get(btitle=brand) # 查询brand下的所有未删除和销售的车辆信息列表 car_list = brand.carinfo_set.filter(ispurchase=0, isdelete=0) # 将传递给模板的变量封装到字典中 params = { 'brand':brand, 'brand_list':brand_list, 'car_list':car_list, } except ObjectDoesNotExist as e: return HttpResponse(e) return render(request, 'list.html', params)
1.2汽车价格分类
def price_car(request, priceLow, priceHigh): '''汽车价格分类''' # 取得页面get请求值 brand = request.GET['brand'] brand_list = Brand.objects.all() # 如果brand为None if brand == 'None': if priceHigh: car_list = Carinfo.objects.filter(isdelete=0, ispurchase=0, price__gt=priceLow, price__lte=priceHigh) else: car_list = Carinfo.objects.filter(isdelete=0, ispurchase=0, price__gt=priceLow) else: brand = Brand.objects.get(btitle=brand) if priceHigh: car_list = brand.carinfo_set.filter(isdelete=0, ispurchase=0, price__gt=priceLow, price__lte=priceHigh) else: car_list = brand.carinfo_set.filter(isdelete=0, ispurchase=0, price__gt=priceLow) params = { 'car_list':car_list, 'brand_list':brand_list, 'brand':brand } resp = render(request, 'list.html', params) return resp def price0_10(request): ''' price0_10 :param request: :return: ''' return price_car(request, 0, 10) def price10_30(request): ''' price10_30 :param request: :return: ''' return price_car(request, 10, 30) def price30_80(request): ''' price30_80 :param request: :return: ''' return price_car(request, 30, 80) def price80_(request): ''' price80_ :param request: :return: ''' return price_car(request, 80, 9999999999) # >>>>>>>>>//有问题 def detail(request): '''汽车详情''' carid = request.GET['carid'] try: car = Carinfo.objects.get(id=carid) except ObjectDoesNotExist as e: return HttpResponse(e) resp = render(request, 'detail.html', {'car':car}) return resp
2.主页热门车型展示
sale-view.py
def index(request): ''' 首页 :param request: :return: ''' # 查询所以品牌信息 brand_list = Brand.objects.all() # 查询所有热卖车型:取出5条待卖车辆记录 car_list = Carinfo.objects.filter(ispurchase=0,isdelete=0) car_five = random.sample(list(car_list), 5) return render(request, 'index.html', locals())
3.汽车详情
sale-view.py
def detail(request): '''汽车详情''' carid = request.GET['carid'] try: car = Carinfo.objects.get(id=carid) except ObjectDoesNotExist as e: return HttpResponse(e) resp = render(request, 'detail.html', {'car':car}) return resp