小编Win*_*hoo的帖子

用于过滤的 Django 搜索表单

我目前正在使用表单进行搜索
这是我的views.py

class HomeView(generic.ListView):
    model = Consultant
    template_name = 'sogeti/home.html'

    def get_queryset(self):
        query = self.request.GET.get('q')
        if query:
            return Consultant.objects.filter(
                Q(first_name__icontains=query) |
                Q(last_name__icontains=query) |
                Q(group__title_techGroup__contains=query) |
                Q(practices__title_practice__contains=query)
            )
        else:
            return Consultant.objects.all()
Run Code Online (Sandbox Code Playgroud)

这是我的 home.html

<form action="" method="get" class="form-inline">
                <input type="text" name="q" placeholder="Enter Keyword" value="{{ request.GET.q }}" class="form-control">
                <select name="filter" class="form-control">
                    <option value="all">All</option>
                    <option value="people">People</option>
                    <option value="certification">Certification</option>
                    <option value="skillset">Skillset</option>
                </select>
                <input type="submit" value="Search" class="btn btn-default">
            </form>

<ol style="padding-left: 15px">
                    {% for consultant in object_list %}
                        <li>
                            <a href="{% url 'sogeti:detail' consultant.id %}">{{ consultant.first_name …
Run Code Online (Sandbox Code Playgroud)

python django django-forms django-views

0
推荐指数
1
解决办法
9316
查看次数

标签 统计

django ×1

django-forms ×1

django-views ×1

python ×1