Haystack Django Tables2预期的表或查询集,而不是'str'

时间:2014-03-14 17:34:24

标签: django django-haystack django-tables2

目前使用Haystack 2.1和Tables2(Dev 0.16)来管理使用Elastisearch的查询。当我尝试搜索时,我得到以下错误消息:预期的表或查询集,而不是' str'。细节在这里

在模板C:\ RealPython \ quorum \ templates \ search \ search.html中,第20行出错 预期的表格或查询集,而不是' str'。

10  <td>&nbsp;</td>
11  <td>
12  <input type="submit" value="Search">
13  </td>
14  </tr>
15  
16  </table>
17  
18  {% if query %}
19  <h3>Results</h3>
20  {% render_table table %}
21  {% for result in page.object_list %}
22  <p>
23  
24  <a href="{{ result.object.get_absolute_url }}">{{ result.object.Inducer_Name_Synonym_Abbreviation }}</a>
25  </p>
26  
27  {% empty %}
28  
29  {% endfor %}
30 

我的views.py代码在这里:

def report(request, template='search/search.html', load_all=True, form_class=ModelSearchForm, searchqueryset=None, context_class=RequestContext, extra_context=None, results_per_page=None):
    table = ''
    query = ''
    results = EmptySearchQuerySet()

    if request.GET.get('q'):
        form = form_class(request.GET, searchqueryset=SearchQuerySet().models(Quorum_Sensing), load_all=load_all)

        if form.is_valid():
            query = form.cleaned_data['q']
            results = form.search()
            pklist = [r.pk for r in results]
            table = QuorumTable(Quorum_Sensing.objects.filter(pk__in=pklist))
            RequestConfig(request).configure(table)

    else:
        form = form_class(searchqueryset=searchqueryset, load_all=load_all)

    context = {
        'form': form,
        'table': table,
    }

    return render(request, template, context)

def quorum_list(request):
    quorum_list = QuorumTable(Quorum_Sensing.objects.filter(pk=1))
    RequestConfig(request).configure(quorum_list)
    return render(request, 'simple_list.html', {"quorum_list": Quorum_Sensing.objects.all()})

如何修复此错误消息?我使用最新的表2和Django 1.62。

提前感谢您的帮助!

2 个答案:

答案 0 :(得分:1)

request.GET.get(q)def reportNone时,您可能会遇到此问题。所以你需要像这样更新report函数的if else块:

if request.GET.get('q'):
    form = form_class(request.GET, searchqueryset=SearchQuerySet().models(Quorum_Sensing), load_all=load_all)
    if form.is_valid():
        query = form.cleaned_data['q']
        results = form.search()
        pklist = [r.pk for r in results]
        table = QuorumTable(Quorum_Sensing.objects.filter(pk__in=pklist))
            RequestConfig(request).configure(table)

else:
   form = form_class(searchqueryset=searchqueryset, load_all=load_all)
   table = QuorumTavke(Quorum_Sensing.objects.none())

    context = {
        'form': form,
        'table': table,
    }

答案 1 :(得分:0)

您正在使用旧版本的django-tables2。