title: login sessions for django
body: <p>I'm trying to set login sessions within my web app but can not get it to work. I'm new to django and read the documentation for sessions but not making the connection to my web app. All I want from it at the moment is to check if a user is logged in and if not redirect to the login page.</p>
  
  <p>Heres the code that i am trying to incorporate login sessions.</p>
  
  <p>settings.py</p>
  
  <pre><code>INSTALLED_APPS = (
      'django.contrib.admin',
      'django.contrib.auth',
      'django.contrib.contenttypes',
      'django.contrib.sessions',
      'django.contrib.messages',
      'django.contrib.staticfiles',
      'logins',
      'dashboards'
  )
  
  MIDDLEWARE_CLASSES = (
      'django.contrib.sessions.middleware.SessionMiddleware',
      'django.middleware.common.CommonMiddleware',
      'django.middleware.csrf.CsrfViewMiddleware',
      'django.contrib.auth.middleware.AuthenticationMiddleware',
      'django.contrib.messages.middleware.MessageMiddleware',
      'django.middleware.clickjacking.XFrameOptionsMiddleware',
  )
  </code></pre>
  
  <p>urls.py</p>
  
  <pre><code>    from django.conf.urls import patterns, include, url
      from django.conf import settings
      from django.conf.urls.static import static
      from django.contrib import admin
      admin.autodiscover()
  
      urlpatterns = patterns('',
          url(r'^$', 'logins.views.login', name='login'),
          url(r'^accounts/auth/$', 'logins.views.auth_view', name='auth_view'),
          url(r'^accounts/dashboard/$', 'dashboards.views.dashboard', name='dashboard'),
          url(r'^accounts/logout/$', 'logins.views.logout', name='logout'),
          url(r'^accounts/invalid/$', 'logins.views.invalid', name='invalid'),
  </code></pre>
  
  <p>views.py for logins</p>
  
  <pre><code>    from django.shortcuts import render, render_to_response, RequestContext
      from django.http import HttpResponseRedirect, HttpResponse
      from django.contrib import auth
      from django.core.context_processors import csrf
  
      def login(request):
          c = {}
          c.update(csrf(request))
          return render_to_response('login.html', c)
  
      def auth_view(request):
          username = request.POST.get('username', '')
          password = request.POST.get('password', '')
          user = auth.authenticate(username=username, password=password)
  
          if user is not None and user.is_active:
              auth.login(request, user)
              return HttpResponseRedirect('/accounts/dashboard')
          else:
              return HttpResponseRedirect('/accounts/invalid')
  
      def logout(request):
          auth.logout(request)
          return render_to_response('logout.html')
  
      def invalid(request):
          return render_to_response('invalid.html')
  </code></pre>
  
  <p>views.py for dashboard app thats being logged into</p>
  
  <pre><code>from django.shortcuts import render, render_to_response, RequestContext
  from django.http import HttpResponseRedirect, HttpResponse
  from django.contrib import auth
  from django.core.context_processors import csrf
  
  def dashboard(request):
      return render_to_response('dashboard.html')
  </code></pre>