我正在尝试在Django模板中打印Datatable。我正在使用Django 1.7并使用metro风格的CSS。如果我尝试从视图返回输出为JSON,它只打印JSON输出而没有任何HTML格式。如果我从视图返回非JSON输出,它会打印带有标题但没有值的HTML和Datatable,IE中的f12调试工具会产生JSON错误。我的目标是打印一个没有任何自定义的基本数据表,我使用ServerSide ajax处理从数据库(模型)中提取数据,我已经尝试在Javascript中使用sAjaxsource。这是我的模板:
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<link href="/static/fnsbuild/metro/docs/css/metro-bootstrap.css" rel="stylesheet" />
<link href="/static/fnsbuild/metro/docs/css/metro-bootstrap-responsive.css" rel="stylesheet" />
<link href="/static/fnsbuild/metro/docs/css/iconFont.css" rel="stylesheet" />
<link href="/static/fnsbuild/metro/docs/css/docs.css" rel="stylesheet" />
<link href="CSS/Override.css" rel="stylesheet" />
<script type="text/javascript" src="/static/fnsbuild/metro/docs/js/jquery/jquery.min.js"></script>
<script type="text/javascript" src="/static/fnsbuild/metro/docs/js/jquery/jquery.dataTables.js"></script>
<script type="text/javascript" src="/static/fnsbuild/metro/docs/js/jquery/jquery.widget.min.js"></script>
<script type="text/javascript" src="/static/fnsbuild/metro/docs/js/jquery/jquery.dataTables.min.js"></script>
<script src="/static/fnsbuild/metro/docs/js/metro.min.js"></script>
<script src="/static/fnsbuild/metro/docs/js/load-metro.js"></script>
<script src="/static/fnsbuild/metro/docs/js/docs.js"></script>
<title>Metro UI CSS : Simple responsive css framework</title>
</head>
<body class="metro">
<div class="container">
<table class="table striped hovered dataTable" id="example">
<thead>
<tr>
<th class="text-left">Engineer</th>
<th class="text-left">Site_Code</th>
<th class="text-left">NSSA</th>
<th class="text-left">Region</th>
<th class="text-left">GFSD</th>
</tr>
</thead>
<tbody>
</tbody>
</table>
<script type="text/javascript" language="javascript" class="init">
$(document).ready(function() {
var oTable=$('#example').DataTable( {
"aaSorting": [[ 2, "asc" ]],
"bprocessing": true
"bServerSide": true,
"ajax": "{% url 'api' %}",
}
);
});
</script>
</div>
这是我的观点:
from django_datatables_view.base_datatable_view import BaseDatatableView
class MyAjaxView(BaseDatatableView):
template_name = 'fnsbuild/table_test.html'
model = OspfNssa
columns = ['id', 'site_code', 'nssa', 'region', 'gfsd']
def render_column(self, row, column):
return super(MyAjaxView, self).render_column(row, column)
URL.py url(r'^ fnsbuild / table_test / $',fnsbuild.views.MyAjaxView.as_view(),name =“api”),
而不是打印数据和值,它只打印JSON输出
{"recordsTotal": 79, "recordsFiltered": 79, "draw": 0, "data": [[56, "by2", "0.0.3.187", "West NA", 1111], [57, "sn3", "0.0.1.24", "Central NA", 1111], [58, "cpq01", "0.0.12.39", "South America", 1111], [59, "hk2", "0.0.4.178", "Asia", 1111], [60, "co1", "0.0.3.32", "West NA", 1111], [61, "bl4", "0.0.1.169", "East NA", 1111], [62, "co2", "0.0.3.37", "West NA", 1111], [63, "kaw", "0.0.4.76", "Asia", 1111], [64, "ch1", "0.0.0.201", "Central NA", 1111], [65, "bn3", "0.0.0.203", "East NA", 1111]], "result": "ok"}
我已尝试过各种论坛上建议的几乎所有内容,几乎放弃了。我是Django的新手所以我可能会遗漏一些愚蠢的东西。请建议。
答案 0 :(得分:0)
来自django-datatables-view
BaseDatatableView
documentation:
django-datatables-view简化了排序,过滤和创建JSON输出的处理
因此,您的视图会生成JSON,如果在AJAX请求中使用它,则会出现这种情况。
如果你需要不同的东西,你应该让MyAjaxView
不从BaseDatatableView
继承,或者构建另一个用ajax请求做一些合理的视图。