pythondjangosphinxdjango-sphinx

Django 'NoneType' object has no attribute '__getitem__'


I read this article http://eshlox.net/en/2012/09/13/sphinxsearch-and-django-ubuntu/

In view i get error for code: total = query_results['total']

error: 'NoneType' object has no attribute 'getitem'

def search(request):
    if request.GET:
        form = SearchForm(request.GET)
        query = request.GET.get('q', '')
        s = SphinxClient()
        s.SetServer('localhost', 9312)
        s.SetLimits(0, 16777215)
        if s.Status():
            query_results = s.Query(query)
            total = query_results['total']
            pages_id = [page['id'] for page in query_results['matches']]
            if pages_id:
                results = Page.objects.filter(id__in=pages_id)
            else:
                results = None
            if results:
                paginator = Paginator(results, 25)
                page = request.GET.get('page')
                try:
                    results = paginator.page(page)
                except PageNotAnInteger:
                    results = paginator.page(1)
                except EmptyPage:
                    results = paginator.page(paginator.num_pages)
            return render(request, 'wiki/search.html',
                          {'results': results,'total': total,
                           'query': query, 'form': form})
        else:
            logger = logging.getLogger('helper')
            logger.error('Sphinxsearch Error! %s' % s.GetLastError())
            messages.add_message(request, messages.ERROR, 'Search server is '
                                 'not responding. Administrator '
                                 'has been informed.')
            form = SearchForm()
            return render(request, 'wiki/search.html', {'form': form})
    else:
        form = SearchForm()
        return render(request, 'wiki/search.html', {'form': form})

Traceback Switch to copy-and-paste view

/usr/local/lib/python2.7/dist-packages/django/core/handlers/base.py in

get_response

                        response = wrapped_callback(request, *callback_args, **callback_kwargs)

    ...
▶ Local vars
/home/max/askmoiseev/ask/views.py in search

              total = query_results['total']

    ...
▶ Local vars

Please tell me what could be the error?


Solution

  • I imagine your query results are None

    query_results = s.Query(query)
    

    so when you try to access

    total = query_results['total']
    

    you get the __getitem__ error because None is not a List.

    Here's an example from the interpreter.

    >>> n = None
    >>> n['b']
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    TypeError: 'NoneType' object has no attribute '__getitem__'
    >>> 
    

    Try running the query in the shell - django-admin.py shell then

    s = SphinxClient()
    s.SetServer('localhost', 9312)
    s.SetLimits(0, 16777215)
    query_results = s.Query(query)
    

    Good luck, Sam