python - “WSGIRequest”对象没有属性 'update'

标签 python django

我已经针对类似问题尝试过一些解决方案,但没有一个有效:

'WSGIRequest' object has no attribute 'session'

'WSGIRequest' object has no attribute 'facebook'

我正在构建一个搜索页面,如下所示:

View .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'),

我之前在另一个项目中做过同样的事情,而且效果很好。

最佳答案

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

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

关于python - “WSGIRequest”对象没有属性 'update',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24082248/

相关文章:

python - 如何在 Pandas 中比较前一行并使用 group by 更改另一列的值?

Python 猎鹰框架 : optional URI parameters

python - 如何正确安装pyinstaller? (我究竟做错了什么?)

python - django-auth-ldap - 查找用户是否属于 ldap 组

python - 为了解决经度/纬度 SQL 查询,我从 PHP 复制到了新的 Python 后端。需要一些第二意见

python - 最后五分钟的 Pandas 滚动总和

django - 如何对 Django 'F' 类型进行算术运算?

python - 如何一次获得所有 Django 外键对象?

django - 使用 django-allauth 时如何自定义用户配置文件

python - PyQt 无法使用 text() 从 QlineEdit 获取文本