我正在尝试在我的一个视图中设置过滤器查询...基本上我的代码如下所示:
def inventory(request):
vehicle = Vehicle.objects.all().exclude(status__status='Incoming').order_by('common_vehicle__series__model__manufacturer__manufacturer', 'common_vehicle__series__model__model', 'common_vehicle__year')
year_count = Vehicle.objects.exclude(status__status='Incoming').order_by('-common_vehicle__year__year').values('common_vehicle__year__year').annotate(count=Count('id'))
make_count = Vehicle.objects.exclude(status__status='Incoming').order_by('common_vehicle__series__model__manufacturer__manufacturer').values('common_vehicle__series__model__manufacturer__manufacturer').annotate(count=Count('id'))
return render_to_response('vehicles.html', {'vehicle': vehicle, 'make_count': make_count, 'year_count': year_count,})
def year_filter(request, year):
vehicle = Vehicle.objects.filter(common_vehicle__year__year=year)
return render_to_response('filter.html', {'vehicle':vehicle,})
def make_filter(request, make):
vehicle = Vehicle.objects.filter(common_vehicle__series__model__manufacturer__manufacturer=make).exclude(status__status='Incoming')
return render_to_response('filter.html', {'vehicle':vehicle,})
到目前为止,当我尝试最后两个视图中的任何一个时,我只从第一个视图中获取查询集,即库存。 URLConf文件如下所示:
(r'^inventory/year/(?P<year>d{4})/?$', 'app.vehicles.views.year_filter'),
(r'^inventory/make/(?P<make>)/', 'app.vehicles.views.make_filter'),
答案 0 :(得分:1)
在我看来,你错过了一个表达。
(r'^inventory/year/(?P<year>d{4})/?$', 'app.vehicles.views.year_filter'),
(r'^inventory/make/(?P<make>)/', 'app.vehicles.views.make_filter'),
<year>
匹配一系列4位数,<make>
匹配的是什么?
(r'^inventory/make/(?P<make>[-\w]+)/', 'app.vehicles.views.make_filter'),
匹配make变量。很高兴知道我错了!
答案 1 :(得分:0)
看起来在您的URLConf中,在您已经提到的那些之上,您可能有一个映射覆盖下面的映射 - 比如说 -
(r'^inventory/', 'app.vehicles.views.inventory'),
导致广告资源中的所有调用都会触及方法广告资源。
这是因为Django以连续的方式完成了它。任何首先匹配它的URL都会点击它。 要解决此问题,请按以下方式重新排序网址 -
(r'^inventory/year/(?P<year>d{4})/?$', 'app.vehicles.views.year_filter'),
(r'^inventory/make/(?P<make>)/', 'app.vehicles.views.make_filter'),
(r'^inventory/', 'app.vehicles.views.inventory'),
答案 2 :(得分:0)
古兰经走在正确的道路上。尝试在你的URLconf中使用它:
(r'^inventory/$', 'app.vehicles.views.inventory'),
(r'^inventory/year/(?P<year>d{4})/?$', 'app.vehicles.views.year_filter'),
(r'^inventory/make/(?P<make>)/', 'app.vehicles.views.make_filter'),
regex r'^ inventory / $'严格匹配,没有超过'/'。这应该会导致其他URL映射到正确的视图。
答案 3 :(得分:0)
d {4}总是恰好匹配四个,dddd
。这就是你想要的吗?
(r'^inventory/year/(?P<year>d{4})/?$', 'app.vehicles.views.year_filter'),
我怀疑您需要使用d
转义\
,比较:
(r'^inventory/year/(?P<year>\d{4})/?$', 'app.vehicles.views.year_filter'),