title: Django admin login returns Forbidden 403 CSRF verification failed. Request aborted
body: <p>Pretty new to Django. Working through a second project following the Polls tutorial on Django website. Previous effort went well, albeit simple. This time around encountering problems accessing admin login.</p>
  
  <p>I have created a superuser and using those credentials, when I try to login to <code>http://127.0.0.1:8000/admin/login/?next=/admin/</code> I get the following error:</p>
  
  <pre><code>Forbidden (403)
  CSRF verification failed. Request aborted.
  Reason given for failure:
      CSRF cookie not set.
  </code></pre>
  
  <p>Looking at <a href="https://stackoverflow.com/questions/3678238/why-is-django-admin-login-giving-me-403-csrf-error">this</a> and <a href="https://stackoverflow.com/questions/29573163/django-admin-login-suddenly-demanding-csrf-token">this</a>, most answers either detail clearing browser cookies (did that), include <code>'django.middleware.csrf.CsrfViewMiddleware'</code> in your middleware (which I do), or creating an exemption or workaround. </p>
  
  <p>1) My question is why the admin portal does not seem to work now, but it did for my previous project and I am following the same steps?</p>
  
  <p>2) Shouldn't the properties for the admin panel be inherited through the project initiation?</p>
  
  <p>3) How would I set the CSRF for admin when <a href="https://docs.djangoproject.com/en/1.9/ref/csrf/" rel="nofollow noreferrer">the documentation</a> appears to state that the CSRF middleware is activated by default?</p>
  
  <p>Thanks for any help.</p>
  
  <p><strong>settings.py</strong></p>
  
  <pre><code>"""
  Django settings for aptly project.
  
  Generated by 'django-admin startproject' using Django 1.9.7.
  
  For more information on this file, see
  https://docs.djangoproject.com/en/1.9/topics/settings/
  
  For the full list of settings and their values, see
  https://docs.djangoproject.com/en/1.9/ref/settings/
  """
  
  import os
  import dj_database_url
  
  from .secret_settings import *
  
  # Build paths inside the project like this: os.path.join(BASE_DIR, ...)
  BASE_DIR = os.path.dirname(os.path.dirname(__file__))
  
  PROJECT_ROOT = os.path.abspath(os.path.dirname(__file__))
  PROJECT_DIR = os.path.join(PROJECT_ROOT,'../search')
  
  
  # Quick-start development settings - unsuitable for production
  # See https://docs.djangoproject.com/en/1.9/howto/deployment/checklist/
  
  # SECURITY WARNING: don't run with debug turned on in production!
  DEBUG = True
  
  ALLOWED_HOSTS = []
  
  
  
  # Application definition
  
  INSTALLED_APPS = [
      'django.contrib.admin',
      'django.contrib.auth',
      'django.contrib.contenttypes',
      'django.contrib.sessions',
      'django.contrib.messages',
      'django.contrib.staticfiles',
      'search',
  ]
  
  MIDDLEWARE_CLASSES = [
      'django.middleware.security.SecurityMiddleware',
      'django.contrib.sessions.middleware.SessionMiddleware',
      'django.middleware.common.CommonMiddleware',
      'django.middleware.csrf.CsrfViewMiddleware',
      'django.contrib.auth.middleware.AuthenticationMiddleware',
      'django.contrib.auth.middleware.SessionAuthenticationMiddleware',
      'django.contrib.messages.middleware.MessageMiddleware',
      'django.middleware.clickjacking.XFrameOptionsMiddleware',
  ]
  
  ROOT_URLCONF = 'aptly.urls'
  
  TEMPLATES = [
      {
          'BACKEND': 'django.template.backends.django.DjangoTemplates',
          'DIRS': [],
          'APP_DIRS': True,
          'OPTIONS': {
              'context_processors': [
                  'django.template.context_processors.debug',
                  'django.template.context_processors.request',
                  'django.contrib.auth.context_processors.auth',
                  'django.contrib.messages.context_processors.messages',
              ],
          },
      },
  ]
  
  WSGI_APPLICATION = 'aptly.wsgi.application'
  
  
  # Database
  # https://docs.djangoproject.com/en/1.9/ref/settings/#databases
  
  DATABASES = {
      'default': {
          "ENGINE": "django.db.backends.postgresql_psycopg2",
          "NAME": "db_name",
          "USER": "me",
          "PASSWORD": "",
          "HOST": "localhost",
          "PORT": "",
      }
  }
  
  # Password validation
  # https://docs.djangoproject.com/en/1.9/ref/settings/#auth-password-validators
  
  AUTH_PASSWORD_VALIDATORS = [
      {
          'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator',
      },
      {
          'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator',
      },
      {
          'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator',
      },
      {
          'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator',
      },
  ]
  
  
  # Internationalization
  # https://docs.djangoproject.com/en/1.9/topics/i18n/
  
  LANGUAGE_CODE = 'en-us'
  
  TIME_ZONE = 'UTC'
  
  USE_I18N = True
  
  USE_L10N = True
  
  USE_TZ = True
  
  
  # Static files (CSS, JavaScript, Images)
  # https://docs.djangoproject.com/en/1.9/howto/static-files/
  
  STATIC_URL = '/static/'
  STATIC_ROOT = os.path.join(BASE_DIR, 'static_root')
  
  # Extra places for collectstatic to find static files.
  STATICFILES_DIRS = (
      os.path.join(PROJECT_DIR, 'static'),
  )
  
  # Simplified static file serving.
  # https://warehouse.python.org/project/whitenoise/
  
  STATICFILES_STORAGE = 'whitenoise.django.GzipManifestStaticFilesStorage'
  
  #DATABASES['default'] = dj_database_url.config()
  </code></pre>
  
  <p><strong>urls.py</strong></p>
  
  <pre><code>from django.conf.urls import patterns, include, url
  from django.contrib import admin
  from . import views
  
  urlpatterns = [
      url(r'^$', views.index, name='index'),
      url(r'^admin/', admin.site.urls),
  ]
  </code></pre>
  
  <p><strong>Directory</strong></p>
  
  <pre><code>project
  -aptly
  --settings.py
  --urls.py
  --wsgi.py
  -search
  --templates
  ---index.html
  --models.py
  --urls.py
  --views.py
  manage.py
  </code></pre>