title: django - HttpRequest object has no attribute 'session'
body: <p>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.</p>
  
  <p>In my views.py:</p>
  
  <pre><code>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
  </code></pre>
  
  <p>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.</p>
  
  <p>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:</p>
  
  <pre><code>(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)
  &gt;&gt;&gt; from django.http import HttpRequest
  &gt;&gt;&gt; r = HttpRequest()
  &gt;&gt;&gt; r.session
  Traceback (most recent call last):
    File "&lt;console&gt;", line 1, in &lt;module&gt;
  AttributeError: 'HttpRequest' object has no attribute 'session'
  &gt;&gt;&gt;
  </code></pre>
  
  <p>What am I missing??</p>
  
  <p>UPDATE: This only happens when testing with unit tests. Here is the test that causes the exception:</p>
  
  <pre><code>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)
  </code></pre>