django - HttpRequest object has no attribute '

2019-06-26 01:36发布

问题:

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.

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

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.

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'
>>>

What am I missing??

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)

回答1:

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

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

Then this should work:

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)


回答2:

The solution is to create the session attribute yourself on the HttpRequest object before passing it to the view function. This is because the HttpRequest Factory doesn't support middleware in unit tests, as per the answer in: Using session object in Django unit test



回答3:

For testing purposes, it may suffice to just use a Mock object which produces the behaviour you need to test the view or another isolated part without involving code you don't want to actually test (like the session middleware).