如何在django-datatable-view中呈现表? [英] How to render tables in django-datatable-view?
问题描述
我无法弄清楚如何使用 Django-datatable-view
呈现可编辑的表格。我想创建一个完全如下的表格:
作为第一步,尝试运行被掩埋的示例项目in:
django-datatable-view / datatableview / t ests / example_project / example_project / example_app
为此,请按照文档和实时演示,但一定要替换在开始之前, django> = 1.4
与 django == 1.8
。也就是说,在我的经验中它与django1.9或1.10不兼容。
完成此操作后,您将拥有一整套工作示例和匹配文档,这将简化一些操作。你给的链接已经过时了。浏览示例( example_app / views.py
)和文档。
当您准备离开沙箱时,请按照以下路线:
-
开始一个新的virtualenv
-
克隆这个分支:
https://github.com/jangeador/django-datatable -view /
(它有更多的提交,使其与最近版本的django兼容)和pip安装(如果您打算进行更改,请使用-e可编辑选项)。 -
遵循此示例可数据对象和元从您运行的示例应用程序。在代码中:
class MyDatatable(Datatable):
class Meta:
model = Entry
列= ['id','headline','pub_date','n_comments','n_pingbacks']
ordering = ['-id']
page_length = 5
search_fields = [ 'blog__name']
unsortable_columns = ['n_comments']
hidden_columns = ['n_pingbacks']
struct_template ='datatableview / default_structure.html'
class ConfigureDatatableObjectDatatableView DatatableView):
model = Entry
datatable_class = MyDatatable
如果您不需要(可选),您可以删除 Datatable
类中的所有属性。
除文档外,我不得不在 DataTableView
类上重写以下方法,使其工作。
def get_template_names(self):
returnexample_base.html
这是您的模板文件,需要包含:
< script src ={%static'path-to / datatables.min.js'%}type =text / javascript>< / script>
< link href ={%static'your-path-to / datatables.min.css'%} =stylesheet>
< script type =text / javascriptsrc ={%static'js / datatableview.js'%}>< / script>
{{datatable}}
< script>
//页面javascript
datatableview.auto_initialize = true;
< / script>
这里值得注意的是包含 datatableview.js
从django-datatable-view模块(以及随附的 datatableview.auto_initialize = true;
)。如果你对datatables.js很开心,你可以自己去配置它,但是给你也是django-datatable-view的新手,这可能是最简单的路由。
I can't figure out how to render an editable table using Django-datatable-view
. I want to create a table exactly like this: http://django-datatable-view.appspot.com/x-editable-columns/ from model City
for example.
I've read the docs and tutorials but still can't figure out how to create the table.
This is what I've done so far:
{% extends "base.html" %}
{% block head %}
{% load static %}e
<link rel="stylesheet" href="https://cdn.datatables.net/1.10.12/css/dataTables.bootstrap.min.css">
<script src="https://cdn.datatables.net/1.10.12/js/jquery.dataTables.min.js"></script>
<script src="https://cdn.datatables.net/1.10.12/js/dataTables.bootstrap.min.js"></script>
<script src="{% static "datatable/js/datatableview.min.js" %}"></script>
<script>
datatableview.auto_initialize = false;
$(function () {
var xeditable_options = {};
datatableview.initialize($('.datatable'), {
fnRowCallback: datatableview.make_xeditable(xeditable_options),
});
})
</script>
{% endblock %}
{% block content %}
{{ datatable }}
{{ object_list }}
{% endblock %}
And my view is:
class ZeroConfigurationDatatableView(dtv_views.XEditableDatatableView):
model = dolava_models.City
datatable_options = {
'columns': [
'id',
("Name", 'name', dtv_helpers.make_xeditable),
("Country", 'country', dtv_helpers.make_xeditable),
]
}
Unfortunately my code renders this:
As the first step, try to run the example project that is buried in:
django-datatable-view/datatableview/tests/example_project/example_project/example_app
To do this, follow the Documentation and Live Demos on the projects github page, but be sure to replace django>=1.4
with django==1.8
before you start. That is, it is not compatible with django1.9 or 1.10 in my experience.
Once you accomplish this, you will have a full set of working examples and the matching documentation which will ease things a bit. The link that you gave is out of date. Explore the examples (example_app/views.py
), and the documentation.
When you are ready to get out of the sandbox, follow this route:
Start a new virtualenv
Clone this branch:
https://github.com/jangeador/django-datatable-view/
(it has a few more commits that make it compatible with recent versions of django), and pip install (use the -e editable option if you plan to make changes).Follow of this example Datatable object and Meta from the example app that you run. In code:
class MyDatatable(Datatable): class Meta: model = Entry columns = ['id', 'headline', 'pub_date', 'n_comments', 'n_pingbacks'] ordering = ['-id'] page_length = 5 search_fields = ['blog__name'] unsortable_columns = ['n_comments'] hidden_columns = ['n_pingbacks'] structure_template = 'datatableview/default_structure.html' class ConfigureDatatableObjectDatatableView(DatatableView): model = Entry datatable_class = MyDatatable
You can delete all attributes in the Datatable
class if you don't need (they are optional).
Apart from the documentation I had to override the following method on the DataTableView
class to make it work.
def get_template_names(self):
return "example_base.html"
This is your template file, which needs to contain:
<script src="{% static 'path-to/datatables.min.js' %}" type="text/javascript"></script>
<link href="{% static 'your-path-to/datatables.min.css' %}" rel="stylesheet">
<script type="text/javascript" src="{% static 'js/datatableview.js' %}"></script>
{{ datatable }}
<script>
// Page javascript
datatableview.auto_initialize = true;
</script>
What is noteworthy here is the inclusion of datatableview.js
from the django-datatable-view module (and the accompanying datatableview.auto_initialize = true;
). If you are comfortable with the datatables.js to begin with, you can go ahead and configure it by yourself, but given you are also new to django-datatable-view also, this is probably the easiest route.
这篇关于如何在django-datatable-view中呈现表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!