I have two models - one for tools and one for parts. The list page will be identical. Can I filter what is shown in the template based on URL?
Views (I'd like to combine tool_list and part_list into product_list)
def tool_list(request):
tools = Tool.objects.all()
parts = Part.objects.all()
return render(request, 'tool_list.html', {'tools': tools, 'parts': parts})
def part_list(request):
parts = Part.objects.all()
tools = Tool.objects.all()
return render(request, 'tool_list.html', {'parts': parts, 'tools': tools})
def product_detail(request, **kwargs):
tool = get_object_or_404(Tool, slug=kwargs.get('slug'))
part = get_object_or_404(Part)
return render(request, 'product_detail.html', {'tool': tool, 'part': part})
url
urlpatterns = [
url(r'^products/tools/$', tool_list, name='tool_list'),
url(r'^products/parts-supplies/$', part_list, name='part_list'),
url(r'^products/(?P<category>[^\.]+)/(?P<slug>[^\.]+)/$', product_detail, name='product_detail'),
]
products/tools...andproducts/parts-supplies...but using the same view template. And, based in theURL, the queryset would be either tools or parts. Now that I think of it, the only way to do this might be to put conditional statements in my template to check for url.