django-HttpRequest对象没有属性"session" [英] django - HttpRequest object has no attribute 'session'

查看:383
本文介绍了django-HttpRequest对象没有属性"session"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我似乎无法使会议正常进行. Django抱怨HttpRequest对象没有名为"session"的属性.在文档中清楚地表明,如果您启用了中间件,并且在已安装的应用程序中启用了django.contrib.sessions,那么您就很好了.我在使用单元测试时遇到此错误.

I can't seem to get sessions working. Django complains that HttpRequest objects have no attribute called 'session'. In the documentation it clearly states that if you have the middleware enabled, and the django.contrib.sessions in your installed apps, then you're good to go. I am getting this error using unit tests.

在我的views.py中:

In my views.py:

def home_page(request):
    response = render(request, 'home.html', {'message_text' : request.session.get('message_text', ''),
'ip_address'    :   request.session.get('ip_address', ''),
'port_number'   :   request.session.get('port_number', ''),
'command_text'  :   request.session.get('command_text', ''),})

    request.session['message_text'] = ''

    return response

我要获取的会话值是我试图在views.py中其他地方的表单发布方法中设置的值.

The session values I'm trying to get are ones that I'm trying to set in my form post method elsewhere in views.py.

它还指出默认情况下在新项目上启用这些功能.因此,我创建了一个新的django项目,并在控制台中检查了session属性.这正是我所做的:

It also states that these are enabled by default on new projects. So I created a fresh new django project and checked for the session attribute in the console. Here's exactly what I did:

(django1.5)Python $ django-admin.py startproject testing
(django1.5)Python $ cd testing/
(django1.5)testing $ python manage.py shell
Python 2.7.3 (v2.7.3:70274d53c1dd, Apr  9 2012, 20:52:43) 
[GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
(InteractiveConsole)
>>> from django.http import HttpRequest
>>> r = HttpRequest()
>>> r.session
Traceback (most recent call last):
  File "<console>", line 1, in <module>
AttributeError: 'HttpRequest' object has no attribute 'session'
>>>

我想念什么?

更新:这仅在使用单元测试进行测试时发生.这是导致异常的测试:

UPDATE: This only happens when testing with unit tests. Here is the test that causes the exception:

def test_home_page_returns_correct_html(self):
        request = HttpRequest()
        response = home_page(request)
        expected_html = render_to_string('home.html')
        self.assertEqual(response.content, expected_html)

推荐答案

将以下内容添加到测试文件的顶部:

Add in the following to the top of your tests file:

from django.conf import settings
from django.utils.importlib import import_module

然后这应该工作:

def test_home_page_returns_correct_html(self):
    request = HttpRequest()
    engine = import_module(settings.SESSION_ENGINE)
    session_key = None
    request.session = engine.SessionStore(session_key)
    response = home_page(request)
    expected_html = render_to_string('home.html')
    self.assertEqual(response.content, expected_html)

这篇关于django-HttpRequest对象没有属性"session"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆