I am trying to use autocomplete using haystack solr. And I am getting the following error for the code below:
views.py code:
import simplejson as json
from django.http import HttpResponse
from haystack.query import SearchQuerySet
from django.shortcuts import render
from django.template.context_processors import csrf
def autocomplete(request):
request=csrf(request)
content_auto=request.GET.get('q', '')
sqs = SearchQuerySet().autocomplete(content_auto)[:5]
suggestions = [result.title for result in sqs]
# Make sure you return a JSON object, not a bare list.
# Otherwise, you could be vulnerable to an XSS attack.
the_data = json.dumps({
'results': suggestions
})
return HttpResponse(the_data, content_type='application/json')
The error which I am getting is given below(which is mostly for line 10 of views.py):
'dict' object has no attribute 'GET'
Request Method: GET
Request URL: http://127.0.0.1:8000/autocomplete/
Django Version: 1.8.2
Exception Type: AttributeError
Exception Value:
'dict' object has no attribute 'GET'
Exception Location: C:\Users\hp user\PycharmProjects\solrven\solr\views.py in autocomplete, line 10
Python Executable: C:\Users\hp user\solr27\Scripts\python.exe
Python Version: 2.7.9
Python Path:
['C:\\Users\\hp user\\PycharmProjects\\solrven',
'C:\\Users\\hp user\\PycharmProjects\\solrven',
'C:\\WINDOWS\\SYSTEM32\\python27.zip',
'C:\\Users\\hp user\\solr27\\DLLs',
'C:\\Users\\hp user\\solr27\\lib',
'C:\\Users\\hp user\\solr27\\lib\\plat-win',
'C:\\Users\\hp user\\solr27\\lib\\lib-tk',
'C:\\Users\\hp user\\solr27\\Scripts',
'C:\\Python27\\Lib',
'C:\\Python27\\DLLs',
'C:\\Python27\\Lib\\lib-tk',
'C:\\Users\\hp user\\solr27',
'C:\\Users\\hp user\\solr27\\lib\\site-packages']
Server time: Tue, 23 Jun 2015 20:21:34 +0530
Traceback Switch to copy-and-paste view
C:\Users\hp user\solr27\lib\site-packages\django\core\handlers\base.py in get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs) ...
▶ Local vars
C:\Users\hp user\PycharmProjects\solrven\solr\views.py in autocomplete
content_auto=request.GET.get('q', '')
My app url looks like below:
urls.py:
from django.conf.urls import patterns, include, url
from . import views
urlpatterns = [
url(r'^autocomplete/$',views.autocomplete)
]
The first line of your view replaces the
request
variable with a context dict containing a CSRF token. Don't do that.