You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Django搜索表单问题:无结果空白页及筛选功能实现求助

Hey there, let's fix your two issues step by step!


1. Fixing the blank page when no search results exist

Right now, when your query doesn't match any consultants, the object_list is empty, so your template skips the loop entirely—hence the blank page. We can add an {% empty %} block to show a friendly message when there are no matches.

Update your home.html template like this:

<ol style="padding-left: 15px">
    {% for consultant in object_list %}
    <li>
        <a href="{% url 'sogeti:detail' consultant.id %}">{{ consultant.first_name }}, {{ consultant.last_name }}</a>
        </br>
        Technology Group: {{ consultant.group }}
        </br>
        Primary Practice: {{ consultant.practices }}
        <hr style="margin-left: 0">
    </li>
    {% empty %}
    <li>No matching consultants found. Try a different keyword!</li>
    {% endfor %}
</ol>

This gives users clear feedback instead of a confusing blank screen.


2. Implementing category-based search with the dropdown

To use the select dropdown for targeted filtering, we need to capture the selected filter value in the view, adjust the queryset dynamically, and keep the dropdown's selected state after form submission.

Step 1: Update the get_queryset method in views.py

First, grab the filter parameter from the request, then build your search query based on the selected category. Note: I’ll assume your Consultant model has relationships/fields for certifications and skills—adjust the field names to match your actual model!

from django.db.models import Q

class HomeView(generic.ListView):
    model = Consultant
    template_name = 'sogeti/home.html'
    
    def get_queryset(self):
        query = self.request.GET.get('q', '')
        filter_param = self.request.GET.get('filter', 'all')
        
        queryset = Consultant.objects.all()
        
        if query:
            search_query = Q()
            
            # Search names for "People" or "All" filter
            if filter_param == 'all' or filter_param == 'people':
                search_query |= Q(first_name__icontains=query) | Q(last_name__icontains=query)
            
            # Search certifications (adjust field name to match your model)
            if filter_param == 'all' or filter_param == 'certification':
                search_query |= Q(certifications__name__icontains=query)
            
            # Search skills (adjust field name to match your model)
            if filter_param == 'all' or filter_param == 'skillset':
                search_query |= Q(skills__name__icontains=query)
            
            # Include group and practice searches only for "All" filter
            if filter_param == 'all':
                search_query |= Q(group__title_techGroup__contains=query) | Q(practices__title_practice__contains=query)
            
            queryset = queryset.filter(search_query)
        
        return queryset

Step 2: Update the dropdown in home.html to preserve selection

Add the selected attribute to the option that matches the current filter value from the request, so users can see which filter is active:

<select name="filter" class="form-control">
    <option value="all" {% if request.GET.filter == 'all' or not request.GET.filter %}selected{% endif %}>All</option>
    <option value="people" {% if request.GET.filter == 'people' %}selected{% endif %}>People</option>
    <option value="certification" {% if request.GET.filter == 'certification' %}selected{% endif %}>Certification</option>
    <option value="skillset" {% if request.GET.filter == 'skillset' %}selected{% endif %}>Skillset</option>
</select>

The or not request.GET.filter ensures "All" is selected by default when the page first loads.


Quick note: Double-check that the field names in the Q() objects (like certifications__name) match exactly what’s in your Consultant model. If your certification/skill fields are named differently, tweak those parts to fit your setup!

内容的提问来源于stack exchange,提问作者Wing Hang Khoo

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.14 07:07:53