简体   繁体   中英

'WSGIRequest' object has no attribute 'update'

I've gone through some solutions for a similar problem but none of these worked:

'WSGIRequest' object has no attribute 'session'

'WSGIRequest' object has no attribute 'facebook'

I'm building a search page which is as follows:

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)

The complete traceback for this is:

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 is as follows:

 url(r'^search$', views.search, name='search'),

I've done the same thing before with another project and it worked perfectly.

return render_to_response('search.html', dictionary, request)

您将请求作为上下文实例传递,而应该传递django.template.Context实例(尽管您可能希望使用其子类RequestContext )。

You've correctly used the new render shortcut in the last line. In the line before the else clause, though, you've used the older render_to_response and passed request as the last parameter. You should use render there too, with request as the first param:

return render(request, 'search.html', dictionary)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM