from django.shortcuts import render, get_object_or_404 from django.db.models import Q from .models import Service, CloudProvider, Country, ServiceLevel def service_list(request): services = Service.objects.all() cloud_providers = CloudProvider.objects.all() countries = Country.objects.all() service_levels = ServiceLevel.objects.all() # Filter handling if request.GET.get("cloud_provider"): services = services.filter(cloud_provider_id=request.GET.get("cloud_provider")) if request.GET.get("country"): services = services.filter(countries__id=request.GET.get("country")) if request.GET.get("service_level"): services = services.filter(service_level_id=request.GET.get("service_level")) if request.GET.get("search"): query = request.GET.get("search") services = services.filter( Q(name__icontains=query) | Q(description__icontains=query) ) context = { "services": services, "cloud_providers": cloud_providers, "countries": countries, "service_levels": service_levels, } return render(request, "services/service_list.html", context) def service_detail(request, pk): service = get_object_or_404(Service, pk=pk) return render(request, "services/service_detail.html", {"service": service})