数据表Ajax在从文件而不是从变量读取JSON时有效(Django) [英] Datatables Ajax works when reading JSON from file, but not from variable (Django)
问题描述
我已经被这个问题困扰了几天了.我正在尝试将JSON数据馈送至DataTables,但是仅当我使用静态文件作为源时才有效(请参见下面的index.html).
I've been stuck with this problem for a few days now. I'm trying to feed JSON data to DataTables, but it only works if I use static file as source (see below index.html).
index.html
index.html
$(document).ready(function() {
$('#mydata').DataTable( {
"ajax": {
"url": '{% static "myapp/supplier.json" %}', //<= works
{# "url": '{{ suppliers_all }}',#} //<=does not work
"dataSrc": ""
},
"columns": [
{ "data": "name" },
{ "data": "classification" },
]
} );
} );
views.py
def index(request):
suppliers_all = Supplier.objects.all().values('name', 'classification')
suppliers_all = json.dumps(list(suppliers_all))
context = {'suppliers_all': suppliers_all,
}
return render(request, 'myapp/index.html', context)
JSON输出:
[{"classification": "Base Supplier", "name": "Supplier Name1"}, {"classification": "Strategic Supplier", "name": "Supplier Name2"}]
当我使用Django传递的变量{{ suppliers_all }}
时,浏览器调试会返回找不到404错误一个>.我根据网站示例尝试对JSON输出进行硬编码,尝试了多种方法,但是如果不直接从文件中获取,它将永远无法工作.
When I use django passed variable {{ suppliers_all }}
browser debugging returns a 404 not found error. I tried hardcoding JSON output according to websites examples, tried many different ways, but it will never work if it's not fetched directly from file.
更新: 通过使用JSON Httpresponse和url创建新视图解决了问题
Update: Solved problem by creating new view with JSON Httpresponse and url
def supjson(request):
suppliers_all = Supplier.objects.all().values('name', 'classification')
suppliers_all = json.dumps(list(suppliers_all), cls=DjangoJSONEncoder)
context = {'suppliers_all': suppliers_all,
return HttpResponse(suppliers_all, content_type='application/json')
然后将Ajax网址更改为以下内容:
Then changed Ajax url to the following:
"ajax": {
"url": '{% url 'myapp:supjson' %}',
"dataSrc": ""
},
虽然有效,但我感觉它是多余的解决方案,因为我在直接通过变量传递数据时遇到了问题.
While it works, I feel it's redundant solution as I have issues passing data through the variable directly.
更新#2 :问题是由于Django自动转义单个逗号而导致JSON解析无法正常进行.我做了以下更改:
Update #2: Issue was due to Django autoescaping single commas which then made JSON parsing did not work. I did the following changes:
在views.py中添加了mark_safe
Added mark_safe in the views.py
context = {'suppliers_all': mark_safe(suppliers_all),
}
字符串化的JSON,然后在index.html模板中对其进行解析:
Stringified JSON and then Parsed it in index.html template:
$(document).ready(function() {
var json=JSON.stringify({{ suppliers_all }});
$('#mydata').DataTable( {
"data": JSON.parse(json),
推荐答案
也许您不需要从url加载数据.如果您的数据已预加载,请尝试将其直接添加到DataTable.
Perhaps you don't need to load data from url. If your data is preloaded then try to add it directly to DataTable.
$(document).ready(function(){
$('#mydata').DataTable({
"data": JSON.parse('{{ suppliers_all }}'),
"columns": [
{"data": "name"},
{"data": "classification"},
]
});
});
这篇关于数据表Ajax在从文件而不是从变量读取JSON时有效(Django)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!