Note that there are some explanatory texts on larger screens.

plurals
  1. POX-editable inline editing in Django - how to get CSRF protection?
    primarykey
    data
    text
    <p>I am trying to get <a href="http://vitalets.github.com/x-editable/index.html">X-Editable</a> inline editing of a model in Django. I am simply trying to change attributes of a model instance (in this case, the name of a Dataset object).</p> <p>I am not sure how to write the view so that it correctly captures the information from the ajax request:</p> <pre><code>POST /datasets/9/update_name/ { pk: 3 //primary key (record id) value: 'The Updated Name' //new value } </code></pre> <p>Then save the new name to the Dataset object.</p> <p>urls.py</p> <pre><code># ex: /datasets/3/update_name url(r'^(?P&lt;pk&gt;\d+)/update_name/$', update_name , name='update_name'), </code></pre> <p>detail.html</p> <pre><code>&lt;h1 class="page-title center"&gt; &lt;a href="#" id="datasetName"&gt;{{ dataset.name }}&lt;/a&gt; &lt;/h1&gt; &lt;script&gt; $('#datasetName').editable({ type: 'text', pk: {{ dataset.pk }}, url: '{% url 'datasets:update_name' dataset.pk %}', title: 'Edit dataset name' params: { csrf: '{% csrf_token %}'} # // This isn't working }); &lt;/script&gt; </code></pre> <p>views.py</p> <pre><code>def update_name(request, dataset_id): # ... Update Dataset object ... json = simplejson.dumps(request.POST) return HttpResponse(json, mimetype='application/json') </code></pre> <p><strong>EDIT:</strong></p> <p>I believe the problem is that there is no CSRF protection. How can I add this in the X-editable form?</p> <p>** EDIT 2: </p> <p>I have also tried this, as per the docs:</p> <pre><code>&lt;h1 class="page-title center"&gt; &lt;a href="#" id="datasetName"&gt;{{ dataset.name }}&lt;/a&gt; &lt;/h1&gt; &lt;script&gt; // using jQuery 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; } var csrftoken = getCookie('csrftoken'); function csrfSafeMethod(method) { // these HTTP methods do not require CSRF protection return (/^(GET|HEAD|OPTIONS|TRACE)$/.test(method)); } $.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')); } } }); $('#datasetName').editable({ type: 'text', pk: {{ dataset.pk }}, url: '{% url 'datasets:update_name' dataset.pk %}', title: 'Edit dataset name', }); &lt;/script&gt; </code></pre>
    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.
    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