ajax - Django中的X-editable内联编辑-如何获得CSRF保护?

标签 ajax django x-editable

我正在尝试获取 X-Editable在 Django 中对模型进行内联编辑。我只是想更改模型实例的属性(在本例中为 Dataset 对象的名称)。

我不确定如何编写 View ,以便正确捕获来自 ajax 请求的信息:

POST /datasets/9/update_name/
{
    pk:    3            //primary key (record id)
    value: 'The Updated Name' //new value
}

然后将新名称保存到 Dataset 对象。

网址.py
# ex: /datasets/3/update_name
url(r'^(?P<pk>\d+)/update_name/$', update_name ,
    name='update_name'),

详细信息.html
<h1 class="page-title center">
    <a href="#" id="datasetName">{{ dataset.name }}</a>
</h1>
<script>
$('#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
});
</script>

View .py
def update_name(request, dataset_id):   
    # ... Update Dataset object ...
    json = simplejson.dumps(request.POST)
    return HttpResponse(json, mimetype='application/json')  

编辑:

我认为问题在于没有 CSRF 保护。如何在 X 可编辑表单中添加它?

** 编辑 2:

根据文档,我也试过这个:
<h1 class="page-title center">
  <a href="#" id="datasetName">{{ dataset.name }}</a>
</h1>

<script>
// using jQuery
function getCookie(name) {
  var cookieValue = null;
  if (document.cookie && document.cookie != '') {
    var cookies = document.cookie.split(';');
    for (var i = 0; i < 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 && document.cookie != '') {
       var cookies = document.cookie.split(';');
       for (var i = 0; i < 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',
});
</script>

最佳答案

哇,我花了这么多时间在这个问题上!

入围版本将是:

<a href="#" id="projectname{{project.id}}" data-type="text" data-pk="{{project.id}}" data-title="Enter project name" data-url="{% url 'updateproject' project.id %}" data-params="{csrfmiddlewaretoken:'{{csrf_token}}'}">{{ project.name }}</a>

然后,打电话
$('#projectname{{project.id}}').editable();

关于ajax - Django中的X-editable内联编辑-如何获得CSRF保护?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13782351/

相关文章:

javascript - 如何通过key访问json数组

javascript - 如何在cakephp中调用 View 文件中的函数?

c# - 使用 Ajax 更新代码隐藏中运行的进程的进度

javascript - 发布到 Capsule CRM 和 "AJAX Proxy"

使用 JSON 的 Django 多语言文本字段

django - 从 Django 模板中删除换行符

python - 如何向 django 查询添加静态用户字段

javascript - jQuery 保持其他框未编辑状态

angularjs - ng-repeat 与 Controller 为每个表格行 : how do I access x-editable form elements?

php - 在 PHP 中运行 Javascript