在CBV ListView中创建搜索

时间:2019-04-21 13:24:21

标签: django

我正在使用基于类的View ListView为我的项目构建搜索API。我在输入get_queryset所要搜索的值时遇到问题。

ProjectSearchListView:

class ProjectSearchListView(ListView):
    model = Project
    template_name = 'projects/user_project_list.html'
    context_object_name = 'projects'
    paginate_by = 2

    def get_queryset(self):
        query  = request.GET.get('q')
        if query:
            projects = Project.objects.filter(Q(name__contains=query) | Q(description__contains=query)).order_by('-date_created')
        else:
            projects = Project.objects.all().order_by('-date_created')

        return projects

这是我的搜索表:

<form class="form my-2 my-lg-0" method="GET" action="{% url 'search-project' %}"  >

                <div class="input-group">
                    <input class="form-control  " 
                        type="text" 
                        name="q"
                        value="{{ request.GET.q }}" 
                        aria-label="Search" 
                        placeholder="Search">
                    <span class="input-group-btn">
                        <button class="btn btn-outline-success my-2 my-sm-0" type="submit" value="Search">
                            Search
                        </button>
                    </span>
                </div>
            </form>

1 个答案:

答案 0 :(得分:1)

Promise<null>GetTableIndex(txt: string): Promise<number> { return element.all(by.css('.items li')).getText().then((txts)=>{ return txts.indexOf(txt); }); } 的上下文中不存在。

将其更改为GetTableIndex(txt: string): Promise<number> { return element.all(by.css('.items li')).map((item)=>{ return item.getText(); }) .then((txts)=>{ return txts.indexOf(txt); }) }