[Fixed]-Django Q Filtered Results in Separate DIV

1👍

Following up on devxplorer’s suggestion, just add a second queryset to your context dictionary and use that to render your special div.

def plaque_list(request):
    today = timezone.now().date()
    queryset_list = Plaque.objects.active().order_by("first_name")
    if request.user.is_staff or request.user.is_superuser:
        queryset_list = Plaque.objects.all().order_by("first_name")

    query = request.GET.get("q")
    filtered_results = None
    if query:
        filtered_results = queryset_list.filter(
            Q(first_name__contains=query) |
            Q(last_name__icontains=query)
        ).distinct()
    paginator = Paginator(queryset_list, 8)  # Show 25 contacts per page
    page_request_var = "page"
    page = request.GET.get(page_request_var)
    try:
        queryset = paginator.page(page)
    except PageNotAnInteger:
        # If page is not an integer, deliver first page.
        queryset = paginator.page(1)
    except EmptyPage:
        # If page is out of range (e.g. 9999), deliver last page of results.
        queryset = paginator.page(paginator.num_pages)

    context = {
        "object_list": queryset,
        "queryset_list": queryset_list,
        'filtered_results': filtered_results,
        "title": "Locate a Veteran",
        "page_request_var": page_request_var,
        "today": today,
    }
    return render(request, "plaques/plaque_list.html", context)

And then you can loop over filtered_results however you would like in that special div.

👤2ps

Leave a comment