Delete via AJAX results in CSRF token missing or incorrect
<p><strong>Django 1.10</strong></p>

<p>DetailView renders "detail.html".</p>

<p>In "detail.html" I place "Delete" button. In other words, I want users to be able to press "Delete" exactly from "detail.html" when they see the object itself.</p>

<p>So, on pressing "Delete" button I call FrameDelete via AJAX.</p>

<p>Could you help me understand why I get the error: <strong>CSRF token missing or incorrect.</strong></p>

<pre><code>class Frame(models.Model):

    title = models.CharField(max_length=100,
                             null=False,
                             blank=False,
                             default="",
                             unique=True,
                             verbose_name="Заголовок")
    def get_delete_url(self):
        return reverse("frame:frame_delete", kwargs=self.get_kwargs())

class FrameDelete(ActionDeleteMixin, DeleteView):
    model = Frame
    success_url = reverse_lazy("empty")
    template_name = 'general/ajax/ajax_confirm_delete.html'

class ActionDeleteMixin():
    def get_context_data(self, **kwargs):
        context = super(ActionDeleteMixin, self).get_context_data(**kwargs)
        context["action"] = self.object.get_delete_url()
        return context
</code></pre>

<p><strong>js</strong></p>

<pre><code>    function post_delete(){
        $.ajax({
            method: 'POST',
            url: "{{ object.get_delete_url }}",
            success: redirect_to_frame_list,
            error: fail
        });
    }

function fail(jqXHR, textStatus, errorThrown){
    debugger;   
}
</code></pre>

<p><strong>ajax_confirm_delete.html</strong></p>

<pre><code>{% block content %}
    &lt;form action="{{ action }}" method="post"&gt;{% csrf_token %}
        &lt;p&gt;Are you surely want to delete "{{ object }}"?&lt;/p&gt;
        &lt;input id="confirm" type="submit" value="Confirm" /&gt;
    &lt;/form&gt;
{% endblock %}
</code></pre>

<p><strong>In the browser when inspecting element in Chrome:</strong></p>

<pre><code>&lt;form action="/frame/18/delete/" method="post"&gt;&lt;input type="hidden" name="csrfmiddlewaretoken" value="bHZxf62Oa9WrapuacCm8gLVNlY2nJHllfwqPsAHoPO0RS8z8NnhMSv5tnIFQZKPP"&gt;
        &lt;p&gt;Are you surely want to delete "18: Michael's news"?&lt;/p&gt;
        &lt;input id="confirm" type="submit" value="Confirm"&gt;
    &lt;/form&gt;
</code></pre>

<p>**</p>

<p><strong>In the browser when stop at the debugger breakpoint in fail function</strong></p>

<pre><code>"
&lt;!DOCTYPE html&gt;
&lt;html lang="en"&gt;
&lt;head&gt;
  &lt;meta http-equiv="content-type" content="text/html; charset=utf-8"&gt;
  &lt;meta name="robots" content="NONE,NOARCHIVE"&gt;
  &lt;title&gt;403 Forbidden&lt;/title&gt;
  &lt;style type="text/css"&gt;
    html * { padding:0; margin:0; }
    body * { padding:10px 20px; }
    body * * { padding:0; }
    body { font:small sans-serif; background:#eee; }
    body&gt;div { border-bottom:1px solid #ddd; }
    h1 { font-weight:normal; margin-bottom:.4em; }
    h1 span { font-size:60%; color:#666; font-weight:normal; }
    #info { background:#f6f6f6; }
    #info ul { margin: 0.5em 4em; }
    #info p, #summary p { padding-top:10px; }
    #summary { background: #ffc; }
    #explanation { background:#eee; border-bottom: 0px none; }
  &lt;/style&gt;
&lt;/head&gt;
&lt;body&gt;
&lt;div id="summary"&gt;
  &lt;h1&gt;Forbidden &lt;span&gt;(403)&lt;/span&gt;&lt;/h1&gt;
  &lt;p&gt;CSRF verification failed. Request aborted.&lt;/p&gt;


&lt;/div&gt;

&lt;div id="info"&gt;
  &lt;h2&gt;Help&lt;/h2&gt;

    &lt;p&gt;Reason given for failure:&lt;/p&gt;
    &lt;pre&gt;
    CSRF token missing or incorrect.
    &lt;/pre&gt;


  &lt;p&gt;In general, this can occur when there is a genuine Cross Site Request Forgery, or when
  &lt;a
  href="https://docs.djangoproject.com/en/1.10/ref/csrf/"&gt;Django's
  CSRF mechanism&lt;/a&gt; has not been used correctly.  For POST forms, you need to
  ensure:&lt;/p&gt;

  &lt;ul&gt;
    &lt;li&gt;Your browser is accepting cookies.&lt;/li&gt;

    &lt;li&gt;The view function passes a &lt;code&gt;request&lt;/code&gt; to the template's &lt;a
    href="https://docs.djangoproject.com/en/dev/topics/templates/#django.template.backends.base.Template.render"&gt;&lt;code&gt;render&lt;/code&gt;&lt;/a&gt;
    method.&lt;/li&gt;

    &lt;li&gt;In the template, there is a &lt;code&gt;{% csrf_token
    %}&lt;/code&gt; template tag inside each POST form that
    targets an internal URL.&lt;/li&gt;

    &lt;li&gt;If you are not using &lt;code&gt;CsrfViewMiddleware&lt;/code&gt;, then you must use
    &lt;code&gt;csrf_protect&lt;/code&gt; on any views that use the &lt;code&gt;csrf_token&lt;/code&gt;
    template tag, as well as those that accept the POST data.&lt;/li&gt;

    &lt;li&gt;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.&lt;/li&gt;
  &lt;/ul&gt;

  &lt;p&gt;You're seeing the help section of this page because you have &lt;code&gt;DEBUG =
  True&lt;/code&gt; in your Django settings file. Change that to &lt;code&gt;False&lt;/code&gt;,
  and only the initial error message will be displayed.  &lt;/p&gt;

  &lt;p&gt;You can customize this page using the CSRF_FAILURE_VIEW setting.&lt;/p&gt;
&lt;/div&gt;

&lt;/body&gt;
&lt;/html&gt;
"
</code></pre>