Django-datatable-view X-editable 内部服务器错误

Django-datatable-view X-editable internal server error

几天来我一直在为这个问题苦苦挣扎。 我想使用 django-datatable-view 的 xeditable 列集成。我的代码正确加载数据 table (see here),但每当我将列指定为 make_xeditable 时,我都会收到 500 内部服务器错误。我看过讨论 django-datatable-view 但 none他们讨论了 x-editable 选项。

使用在线现场演示 (here)(0.7 版)中的片段并没有任何作用。 table 加载但列不是 editable.

class PriceListDataTableView(XEditableDatatableView):
    model = PriceList
    datatable_options = {
        'columns': [
            'id',
            'date',
            'product',
            'unit',
            ("Price", 'price', helpers.make_xeditable),
        ]
}

我在我的本地主机上安装了最新版本 (0.9) 运行ning,他们的示例有效!但我无法让它在我自己的应用程序中运行。两种设置 运行 django 1.8

这是我的模型:

class PriceList(models.Model):

    # Fields
    date =    models.DateField(verbose_name="Price list date")
    product = models.CharField(verbose_name="Product name", max_length=100)
    unit =    models.CharField(verbose_name="Unit", max_length=6)
    price =   models.DecimalField(verbose_name="Price", decimal_places=2, 
              max_digits=10)

这是我的模板:

{% extends "agrichem/base.html" %}
{% block content %}

<script>
$(document).ready(function() {
    <!-- Initialization for x-editable tables -->
    $.fn.editable.defaults.mode = 'inline';
    $(function(){
        var xeditable_options = {};
        datatableview.initialize($('.datatable'), {
               fnRowCallback: datatableview.make_xeditable(xeditable_options),
        });

    });
});
</script>

{{ datatable }}

{% endblock %}

这是我的观点:

class PriceListDataTableView(XEditableDatatableView):
    model = PriceList

    class datatable_class(Datatable):
        class Meta:
            columns = ['id', 'date', 'product', 'unit', 'price']
            processors = {
                'price': helpers.make_xeditable,

            }

如果我删除 processors 块,我的 table 会加载,但不会加载 editable。有了它,我会看到一个弹出窗口:

DataTables warning: table id=DataTables_Table_0 - Ajax error. For more information about this error, please see datatables.net/tn/7

每当服务器没有 return 2xx 代码时都会发送此弹出错误,所以它基本上是由于以下位而发生的:

在控制台中我收到 500 内部服务器错误(link 计数的破坏原因):

jquery.min.js:4 GET ht__tp://127.0.0.1:8000/pricelist/?draw=1&columns%5B0%5D%5Bdata%5D=0&columns%art=0&length=25&search%5Bvalue%5D=&search%5Bregex%5D=false&_=1497779941842 500 (INTERNAL SERVER ERROR)

我现在正在兜圈子,我需要一些帮助。有人让这个工作吗?

编辑: 我现在已经将 django-datatable-view 降级到 0.8 版,并使用了下面建议的语法,我也得到了同样的 500 内部服务器错误。我怀疑我在某处遗漏了一些基本的设置步骤,但是文档(例如它)没有说明什么。

尝试

class PriceListDataTableView(XEditableDatatableView):
    model = PriceList
    datatable_options = {
        'columns': [
            'id',
            ("Price", 'price', helpers.make_xeditable),
            ...
        ]
    }

编辑:您还需要页面级初始化:

// Page javascript
datatableview.auto_initialize = false;
$(function(){
    var xeditable_options = {};
    datatableview.initialize($('.datatable'), {
        fnRowCallback: datatableview.make_xeditable(xeditable_options),
    });
})

编辑:模板中缺少 csrf 令牌,转载如下:

{% extends "agrichem/base.html" %}
{% block content %}

<script>
$(document).ready(function() {
    <!-- Initialization for x-editable tables -->
    $.fn.editable.defaults.mode = 'inline';
    $(function(){
        var xeditable_options = {};
        datatableview.initialize($('.datatable'), {
           fnRowCallback: datatableview.make_xeditable(xeditable_options),
        });

    });
});
</script>

行是

{% csrf_token %}

{{数据表}}

{% 端块 %}

经过多次挫折和兜圈子后,我放弃了 django-datatable-view。我无法让它用于内联编辑,这是我的主要要求。

然后我找到了django-inplaceedit。它很有魅力,是我需要的一切。

我在模板中渲染 table,如下所示:

{% extends "site_base.html" %}


{% block more_extra_script %}

    {% load inplace_edit %}
    {% inplace_css 0 %}
    {% inplace_js 1 0 %}

{% endblock more_extra_script %}


{% block content %}

<div class="container-fluid">
    <p><a class="btn btn-default" href="{% url 'pricelist_list' %}">PriceList Listing</a></p>

    <h2>Pricelist for {{ pricelist_date }}</h2>
    <p>Double-click a price to edit it</p>

    <table class="table table-responsive table-striped .table-bordered .table_hover">
      <thead class="info">
          <th>Product</th>
          <th>Unit</th>
          <th>Price</th>
      </thead>
      <tbody>
          {% for prod in products %}
              <tr>
                  <td>{{prod.product}}</td>
                  <td>{{prod.unit}}</td>
                  <td>{% inplace_edit "prod.price" %}</td>
              </tr>
          {% endfor %}
      </tbody>
    </table>

</div>
{% endblock %} 

这个应用程序的美妙之处在于它透明地处理所有服务器端的东西。您双击该字段,对其进行更改,然后按回车键,该值将在数据库中更新。