title: CSRF verification failed (Django)
body: <p>I'm getting this error when trying to log in to my django app.</p>
  
  <pre><code>Forbidden (403)
  CSRF verification failed. Request aborted.
  
  You are seeing this message because this site requires a CSRF cookie when submitting forms. This cookie is required for security reasons, to ensure that your browser is not being hijacked by third parties.
  
  If you have configured your browser to disable cookies, please re-enable them, at least for this site, or for 'same-origin' requests.
  
  Help
  Reason given for failure:
  
      CSRF cookie not set.
  
  In general, this can occur when there is a genuine Cross Site Request Forgery, or when Django's CSRF mechanism has not been used correctly. For POST forms, you need to ensure:
  
  - Your browser is accepting cookies.
  - The view function passes a request to the template's render method.
  - In the template, there is a {% csrf_token %} template tag inside each POST form that targets an internal URL.
  - If you are not using CsrfViewMiddleware, then you must use csrf_protect on any views that use the csrf_token template tag, as well as those that accept the POST data.
  - The form has a valid CSRF token. After logging in in another browser tab or hitting the back button after a login, you may need to reload the page with the form, because the token is rotated after a login.
  You're seeing the help section of this page because you have DEBUG = True in your Django settings file. Change that to False, and only the initial error message will be displayed.
  
  You can customize this page using the CSRF_FAILURE_VIEW setting.
  </code></pre>
  
  <p>I've checked everything that I know how to check in that list that's been provided by the error message, and I can't figure out what the problem might be. I suspect it has something to do with the tokens in the templates I'm using. But I can't figure out what exactly is happening. And what does it mean that "CSRF cookie not set"? Here are the relevant parts of my app:</p>
  
  <p>the view:</p>
  
  <pre><code>class LoginView(View):
  
      form_class = LoginForm
      template_name = 'workoutcal/login.html'
  
      def post(self, request):
  
          form = self.form_class(request.POST)
  
          if form.is_valid():
  
              email = form.cleaned_data['email']
              password = form.cleaned_data['password']
  
              user = authenticate(email = email, password = password)
  
              if user is not None:
  
                  if user.is_active:
                      login(request, user)
                      return calendar(request)
              else:
                  return render(request, self.template_name, {'form':form, 'custom_error_message':'The user does not exist'})
          else:
              return render(request, self.template_name, {'form':form})
  
      def get(self, request):
  
          form = self.form_class(None)
  
          return render(request, self.template_name, {'form':form})
  </code></pre>
  
  <p>The template:</p>
  
  <pre><code>{% extends "workout/base.html" %}
  
  {% block logoutwidget %}{% endblock %}
  
  {% block content %}
      &lt;form action="/workoutcal/login/" method="post"&gt;
          {% csrf_token %}
          {% include "workoutcal/form_with_errors.html" %}
          &lt;input type="submit" value="Log in"&gt;
      &lt;/form&gt;
  {% endblock %}
  </code></pre>
  
  <p>form_with_errors.html:</p>
  
  <pre><code>{% csrf_token %}
  &lt;span class="error"&gt;
      {{ custom_error_message }}
      {{ form.non_field_errors }}
  &lt;/span&gt;
  {% for field in form.visible_fields %}
      &lt;div class="row"&gt;
          &lt;div class="col-xs-2"&gt;
              {{ field.label_tag }}
          &lt;/div&gt;
          &lt;div class="col-xs-2"&gt;
              {{ field }}
          &lt;/div&gt;
          &lt;div class="col-xs-3"&gt;
              {{ field.errors }}
          &lt;/div&gt;
      &lt;/div&gt;
  
  {% endfor %}
  </code></pre>
  
  <p>settings:</p>
  
  <pre><code>MIDDLEWARE = [
      'django.middleware.security.SecurityMiddleware',
      'whitenoise.middleware.WhiteNoiseMiddleware',
      '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>EDIT:</p>
  
  <p>Here's all the data that I could find in the developer's tools with cookie and csrf in it:</p>
  
  <p>Request headers:</p>
  
  <pre><code>POST /workoutcal/login/ HTTP/1.1
  Host: localhost:8000
  Connection: keep-alive
  Content-Length: 213
  Cache-Control: max-age=0
  Origin: http://localhost:8000
  Upgrade-Insecure-Requests: 1
  Content-Type: application/x-www-form-urlencoded
  User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36
  Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8
  Referer: http://localhost:8000/workoutcal/login/
  Accept-Encoding: gzip, deflate, br
  Accept-Language: en-US,en;q=0.9,sv;q=0.8
  Cookie: Pycharm-7262e2e=f2e907da-c765-421b-91e2-42fab01cc759
  </code></pre>
  
  <p>form data:</p>
  
  <pre><code>csrfmiddlewaretoken=i6NEr5alSUK4mL7A7ov2HDBMaOErD7uRoW9Q16pKTESrSm1Rg08f9LMYqZcFRCDN&amp;
  csrfmiddlewaretoken=i6NEr5alSUK4mL7A7ov2HDBMaOErD7uRoW9Q16pKTESrSm1Rg08f9LMYqZcFRCDN&amp;
  email=myaddress%40hotmail.com&amp;
  password=mypassword
  </code></pre>
  
  <p>As far as I can see both CSRF token and cookies are there...</p>