我已经解决了类似问题的一些解决方案,但这些都没有奏效:
'WSGIRequest' object has no attribute 'session'
'WSGIRequest' object has no attribute 'facebook'
我正在构建一个搜索页面,如下所示:
views.py
def search(request):
if 'query' in request.GET and request.GET['query']:
q = request.GET['query']
ct = Product.objects.filter(Q(name__icontains=q) | Q(desc__icontains=q) | Q(category__icontains=q))
count = ct
paginate = Paginator(ct, 10)
page = request.GET.get('page')
try:
ct = paginate.page(page)
except PageNotAnInteger:
ct = paginate.page(1)
except EmptyPage:
ct = paginate.page(paginate.num_pages)
dictionary = {'results': ct, 'count': count, 'query': q, }
return render_to_response('search.html', dictionary, request)
else:
content = {'contentnotfound': 'Hi, you did not search for anything. Please go back or use the search box above.'}
return render(request, 'search.html', content)
完整的追溯是:
Environment:
Request Method: GET
Request URL: http://127.0.0.1:8000/search?query=abc&submit=Submit
Django Version: 1.6.4
Python Version: 2.7.3
Installed Applications:
('django_admin_bootstrapped.bootstrap3',
'django_admin_bootstrapped',
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'Abstract',
'south',
'djrill',
'storages',
's3direct')
Installed Middleware:
('django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware')
Traceback:
File "/home/siddharth/AbstractIndia/venv/local/lib/python2.7/site-packages/django/core/handlers/base.py" in get_response
114. response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/home/siddharth/AbstractIndia/Abstract/views.py" in search
39. return render_to_response('search.html', dictionary, request)
File "/home/siddharth/AbstractIndia/venv/local/lib/python2.7/site-packages/django/shortcuts/__init__.py" in render_to_response
29. return HttpResponse(loader.render_to_string(*args, **kwargs), **httpresponse_kwargs)
File "/home/siddharth/AbstractIndia/venv/local/lib/python2.7/site-packages/django/template/loader.py" in render_to_string
167. context_instance.update(dictionary)
Exception Type: AttributeError at /search
Exception Value: 'WSGIRequest' object has no attribute 'update'
urls.py如下:
url(r'^search$', views.search, name='search'),
我之前用另一个项目完成了同样的事情并且它完美地工作了。
答案 0 :(得分:1)
return render_to_response('search.html', dictionary, request)
您将请求作为上下文实例传递,而您应该传递django.template.Context
的实例(尽管您可能希望使用其子类RequestContext
)。
答案 1 :(得分:1)
您已在最后一行正确使用了新的render
快捷方式。但是,在else子句之前的行中,您已使用较旧的render_to_response
并将请求作为最后一个参数传递。您也应该使用render
,请求作为第一个参数:
return render(request, 'search.html', dictionary)