Note that there are some explanatory texts on larger screens.

plurals
  1. POjQuery: setting up CSRF token for Django not working
    primarykey
    data
    text
    <p>my <code>jQuery</code> function looks like</p> <pre><code>$(function() { // activate "New" buttons if input is not empty $('form input[type="text"]').live('keyup', function() { var val = $.trim(this.value); $(this).next("button").prop('disabled', val.length === 0); }); $("body").on("submit","form",function(e){ // do not submit the form e.preventDefault(); // handle everything yourself var $form = $(this); var title = $form.closest('.video-detail').find('.title').text(); var entryTitle = $form.find('.input-small').val(); console.debug(title); console.debug(entryTitle); $.ajaxSetup({ beforeSend: function(xhr, settings) { function getCookie(name) { var cookieValue = null; if (document.cookie &amp;&amp; document.cookie != '') { var cookies = document.cookie.split(';'); for (var i = 0; i &lt; cookies.length; i++) { var cookie = jQuery.trim(cookies[i]); // Does this cookie string begin with the name we want? if (cookie.substring(0, name.length + 1) == (name + '=')) { cookieValue = decodeURIComponent(cookie.substring(name.length + 1)); break; } } } return cookieValue; } if (!(/^http:.*/.test(settings.url) || /^https:.*/.test(settings.url))) { // Only send the token to relative URLs i.e. locally. xhr.setRequestHeader("X-CSRFToken", getCookie('csrftoken')); } } }); // send the data to the server using .ajax() or .post() $.ajax({ type: 'POST', url: 'addVideo', data: { video_title: title, csrfmiddlewaretoken: '{{ csrf_token }}' }, }).done(function(){ alert('done'); }); }); }); </code></pre> <p>This is based on answer <a href="https://stackoverflow.com/questions/5100539/django-csrf-check-failing-with-an-ajax-post-request">Django CSRF check failing with an Ajax POST request</a></p> <p>My <code>html</code> looks like </p> <pre><code>&lt;form class="new-playlist form-inline" onclick="event.stopPropagation()"&gt;{% csrf_token %} &lt;input type="text" class="input-small"&gt; &lt;button class="btn btn-danger create-playlist-button" type="submit" disabled="disabled"&gt;New&lt;/button&gt; &lt;/form&gt; </code></pre> <p>When I debug the code in Firefox, I see post values as</p> <pre><code>csrfmiddlewaretoken {{ csrf_token }} video_title The Who - Who Are You? </code></pre> <p>How can I populate the <code>{{ csrf_token }}</code> value?</p> <p>Thank you</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload