django - HttpRequest object has no attribute 'session'
Asked Answered
P

3

6

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)
Peres answered 31/5, 2013 at 21:2 Comment(0)
F
18

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)
Flashlight answered 10/6, 2013 at 9:18 Comment(1)
django.utils.importlib has been marked as obsolete since Django 1.7 and removed in Django 1.9. If you're on Django >= 1.9, use: from importlib import import_module instead.Ductile
P
1

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

Peres answered 31/5, 2013 at 21:53 Comment(0)
S
0

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).

Shirty answered 7/12, 2014 at 17:23 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.