/autocomplete/ 处的属性错误
AttributeError at /autocomplete/
我正在尝试通过 haystack solr 使用自动完成功能。我收到以下代码的以下错误:
views.py代码:
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')
我得到的错误如下(主要是 views.py 的第 10 行):
'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', '')
我的应用 url 如下所示:
urls.py:
from django.conf.urls import patterns, include, url
from . import views
urlpatterns = [
url(r'^autocomplete/$',views.autocomplete)
]
视图的第一行将 request
变量替换为包含 CSRF 令牌的上下文字典。不要那样做。
我正在尝试通过 haystack solr 使用自动完成功能。我收到以下代码的以下错误:
views.py代码:
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')
我得到的错误如下(主要是 views.py 的第 10 行):
'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', '')
我的应用 url 如下所示:
urls.py:
from django.conf.urls import patterns, include, url
from . import views
urlpatterns = [
url(r'^autocomplete/$',views.autocomplete)
]
视图的第一行将 request
变量替换为包含 CSRF 令牌的上下文字典。不要那样做。