通过 Django 在 bootstrap table (python -> jquery) 中加载 pandas 数据帧
Load pandas dataframe in bootstrap table (python -> jquery) through django
我不确定这里出了什么问题 - 我已经尝试了所有组合但无法让它工作......
我有以下内容。
1/ 在 Python 中使用数据上下文变量(django 框架)创建的视图
def index(request):
# Sim df
df = pd.DataFrame()
df = df.append({'col1': 'col1', 'col2': 'col2'}, ignore_index=True)
# Context
context = {
'data': df.to_json(orient='records'),
}
return render(request, 'index.html', context)
2/ 使用 "bootstrap-table"(index.html 的一部分)
为我的数据帧 table 加载程序
$(function() {
$('#datatable').bootstrapTable({
columns: [{
field: 'col1',
title: 'col1'
}, {
field: 'col2',
title: 'col2'
}],
data: [{col1: '1', col2: 'Item1'}]
});
});
3/ 上面显示了两列和一行,效果很好。但是,如果我替换并使用以下内容,它就不再存在了。没有具体错误,但不显示任何内容 ...
$(function() {
$('#datatable').bootstrapTable({
columns: [{
field: 'col1',
title: 'col1'
}, {
field: 'col2',
title: 'col2'
}],
data: '{{data}}', // loading from context
});
});
如果对某人有帮助,您必须用以下内容替换。
data: {{data|safe}},
并确保 JS 在 index.html 中,而不是在加载的外部脚本文件中。那是我的两个错误...
我不确定这里出了什么问题 - 我已经尝试了所有组合但无法让它工作...... 我有以下内容。
1/ 在 Python 中使用数据上下文变量(django 框架)创建的视图
def index(request):
# Sim df
df = pd.DataFrame()
df = df.append({'col1': 'col1', 'col2': 'col2'}, ignore_index=True)
# Context
context = {
'data': df.to_json(orient='records'),
}
return render(request, 'index.html', context)
2/ 使用 "bootstrap-table"(index.html 的一部分)
为我的数据帧 table 加载程序$(function() {
$('#datatable').bootstrapTable({
columns: [{
field: 'col1',
title: 'col1'
}, {
field: 'col2',
title: 'col2'
}],
data: [{col1: '1', col2: 'Item1'}]
});
});
3/ 上面显示了两列和一行,效果很好。但是,如果我替换并使用以下内容,它就不再存在了。没有具体错误,但不显示任何内容 ...
$(function() {
$('#datatable').bootstrapTable({
columns: [{
field: 'col1',
title: 'col1'
}, {
field: 'col2',
title: 'col2'
}],
data: '{{data}}', // loading from context
});
});
如果对某人有帮助,您必须用以下内容替换。
data: {{data|safe}},
并确保 JS 在 index.html 中,而不是在加载的外部脚本文件中。那是我的两个错误...