title: Django logout not working
body: <p>I seem to have the same problem as in this question: <a href="https://stackoverflow.com/questions/2074980/django-logout-problem">Django logout problem</a></p>
  
  <p>Mine is a bit weirder, it works in google chrome.... but not in firefox...</p>
  
  <p>this is my logout function: (in views.py)</p>
  
  <pre><code>def my_logout(request):
      logger.debug("Logout called by user")
      try:
          # Here I do some custom stuff, like logging this action in a database and so on
          # For this question it shouldn't matter... because in a try catch
          # so whatever goes wrong here, logging out should work anyway
      except Exception, e:
          logger.info("Logging logout action error: %s" % e)
      logout(request)
      return HttpResponseRedirect("/")
  </code></pre>
  
  <p>in settings.py I have:</p>
  
  <pre><code>LOGIN_URL = '/desktop/login/'
  LOGOUT_URL = '/desktop/logout/'
  LOGIN_REDIRECT_URL = '/'
  </code></pre>
  
  <p>And in the urls.py of the app iamapps (include in the project urls as /desktop/):</p>
  
  <pre><code>url(r'^login/$', 'iamapps.views.my_login', name='iamapps.login'),
  url(r'^logout/$', 'iamapps.views.my_logout', name='iamapps.logout'),
  </code></pre>
  
  <p>further info:</p>
  
  <ul>
  <li>django 1.4.3 (just updated from 1.3 to 1.4 ....)</li>
  <li>python 2.7</li>
  <li>works in Chrome but not in Firefox 17.0.1, Linux</li>
  </ul>
  
  <p>The fact that it does work in google chrome but does not work in firefox puzzles me the most. Seems it has something to do with firefox keeps remembering the user as being logged on...</p>
  
  <p><strong>EDIT:</strong>
  I get a broken pipe.... but I seem to get it not on logging out... but going to the home view after logout....</p>
  
  <pre><code>Traceback (most recent call last):
    File "/usr/lib/python2.7/wsgiref/handlers.py", line 86, in run
      self.finish_response()
    File "/usr/lib/python2.7/wsgiref/handlers.py", line 127, in finish_response
      self.write(data)
    File "/usr/lib/python2.7/wsgiref/handlers.py", line 210, in write
      self.send_headers()
    File "/usr/lib/python2.7/wsgiref/handlers.py", line 268, in send_headers
      self.send_preamble()
    File "/usr/lib/python2.7/wsgiref/handlers.py", line 192, in send_preamble
      'Date: %s\r\n' % format_date_time(time.time())
    File "/usr/lib/python2.7/socket.py", line 324, in write
      self.flush()
    File "/usr/lib/python2.7/socket.py", line 303, in flush
      self._sock.sendall(view[write_offset:write_offset+buffer_size])
  error: [Errno 32] Broken pipe
  ----------------------------------------
  Exception happened during processing of request from ('127.0.0.1', 58684)
  Traceback (most recent call last):
    File "/usr/lib/python2.7/SocketServer.py", line 582, in process_request_thread
      self.finish_request(request, client_address)
    File "/usr/lib/python2.7/SocketServer.py", line 323, in finish_request
      self.RequestHandlerClass(request, client_address, self)
    File "/media/storage/django/sites/iamfloraservice/parts/django/django/core/servers/basehttp.py", line 139, in __init__
      super(WSGIRequestHandler, self).__init__(*args, **kwargs)
    File "/usr/lib/python2.7/SocketServer.py", line 641, in __init__
      self.finish()
    File "/usr/lib/python2.7/SocketServer.py", line 694, in finish
      self.wfile.flush()
    File "/usr/lib/python2.7/socket.py", line 303, in flush
      self._sock.sendall(view[write_offset:write_offset+buffer_size])
  error: [Errno 32] Broken pipe
  ----------------------------------------
  [24/Dec/2012 14:33:25] "GET / HTTP/1.1" 200 48247
  </code></pre>
  
  <p><strong>Edit 2</strong>
  it goes to this view after loging out and being redirected:</p>
  
  <pre><code>def home(request, template='iamfloraservice/home.html'):
  
      logger.debug("Home view called by user %s" % request.user)
      return render_to_response(template,{},context_instance=RequestContext(request))
  </code></pre>
  
  <p>I think the redirect with the request to this view causes the problem....
  In the log sais it's still user 'michel' (beacuse the view uses the request from the redirect, and that had user michel)... however... user michel is being logged out in the mean time.... </p>
  
  <p><strong>EDIT 3</strong> </p>
  
  <p>because of the suggestion it's due to the logger.
  unremarking the logging does not help
  And it's the default logger:</p>
  
  <pre><code>import logging
  logger = logging.getLogger(__name__)
  </code></pre>
  
  <p><strong>EDIT 4</strong> (30-12-2012)</p>
  
  <p>My logout is from a main window where I show a logout link when a user is logged on and a login link if a user is logged out. Also it contains a toolbar, which tools are filled depending on the the user and it's membership of groups.</p>
  
  <p>I think the problem is, it's reloading this main window while the cache and the user in it's request isn't cleared yet. Somehow Chrome knows how to handle this, and firefox results in a broken pipe error. Clearing the cache manually in the browser results in the correct view after reload....</p>
  
  <p>a solution might be to redirect to a page without anything in it that contains users...
  or find out to clear the cache on the right moment myselve....</p>
  
  <p>this problem describes maybe the same... but I cannot expect users to do anything in the browser just to logout? see <a href="https://stackoverflow.com/questions/10903809/django-1-4-caching-get-to-login">django 1.4 caching GET to /login/</a></p>
  
  <p><strong>Edit 5</strong> (31-12-2012)</p>
  
  <p>It seems it's a caching problem.... but not sure how to fix this yet.</p>
  
  <p>these are my caching settings:</p>
  
  <pre><code>if not DEBUG:
      CACHES = {
          'default': {
              'BACKEND': 'django.core.cache.backends.memcached.MemcachedCache',
              'LOCATION': '127.0.0.1:11211',
          }
      }
  else:
      CACHES = {
          'default': {
              'BACKEND': 'django.core.cache.backends.dummy.DummyCache',
          }
      }
  </code></pre>
  
  <p>But I tried without the dummycache as well</p>
  
  <p><strong>EDIT 6</strong> (4-jan-2013)
  Still no solution.... I changed the way I log out to the django way, and I Am using the signals now... see my own answer below.</p>
  
  <p>but still it gives the brokenpipe error which causes firefox stopping logging out. It's not a caching problem. If I go to another page, or even worse... the admin pages. I Am still logged on. The only way to logout is through the logout on the admin page.... If it's not an admin user... there is no way to get me logged out on the firefox browser.</p>
  
  <p>When logging off using the admin interface, so the signal works fine...</p>
  
  <p>I checked by turning off the signal.... and still the logging out in firefox does not work.</p>
  
  <p>Conclusion: going back to the main page () is causing the problem.</p>
  
  <p><strong>EDIT 7</strong> (4 jan 2013)
  I made a simple loggedout view for testing,
  this template:</p>
  
  <pre><code>&lt;html&gt;
  &lt;head&gt;
  &lt;title&gt;
  Logged out
  &lt;/title&gt;
  &lt;/head&gt;
  &lt;body&gt;
  You are succesfully logged out.&lt;br&gt;
  &lt;br&gt;
  &lt;a href="/"&gt;Go back to the main page&lt;/a&gt;
  or&lt;br&gt;
  &lt;a href="/desktop/login/?next=/"&gt;log in again&lt;/a&gt;
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>and the logged out view:</p>
  
  <pre><code>class LoggedOutView(TemplateView):
      template_name = "iamapps/logged_out.html"
  </code></pre>
  
  <p>and changed the urls in to:</p>
  
  <pre><code>url(r'^logout/$', 'django.contrib.auth.views.logout',  {'next_page': '/desktop/loggedout/'}, name='iamapps.logout'),
  #url(r'^logout/$', 'django.contrib.auth.views.logout_then_login',  name='iamapps.logout'),
  url(r'^loggedout/$', LoggedOutView.as_view(),name='iamapps.loggedout'),
  </code></pre>
  
  <p>and still, to simplyfy things... I have the signals turned off.</p>
  
  <p>and it's still not working in firefox.... but it works in chrome</p>
  
  <p>In firefox it does not go to the logged out page</p>