将Django模板变量传递给js数据表
我想使用JS数据表库创建表.
Hi I would like to create table using JS datatables library.
将模板中的值传递给js脚本时遇到问题. 我从要显示的表中创建了JSON对象. 它已正确传递给模板,当我显示它时,一切都很好,但是当尝试将其传递给脚本时,什么也没发生,所以我得到了空表.
I got problem when passing value in template to js script. I created JSON object from my table which I want to display. It's passed correctly to template, when I display it everything is fine, but when trying to pass it to script nothing happend and I got empty table.
那是我的方式:
class VotesList(generic.ListView):
model = Vote
template_name = 'votes-list.html'
def get_context_data(self, **kwargs):
votes = Vote.objects.all().values('user', 'group', 'council')
votes_json = json.dumps(list(votes))
context = super(VotesList, self).get_context_data(**kwargs)
context['orderby'] = self.request.GET.get('orderby', 'created_date')
context['json_data'] = votes_json
return context
模板:
{% block javascript %}
{% load static %}
<script type="text/javascript">
$(document).ready(function() {
var json=JSON.parse('{{ json_data | safe }}');
$('#votes_list').DataTable({
data: json,
columns:[
{ title: "user" },
{ title: "group" },
{ title: "council" }]
} );
};
</script>
{% endblock %}
{% block content %}
<p>{{ json_data | safe }}</p> <<< here data is printed fine
{% if vote_list %}
<table id="votes_list" class="display", style="width:100%">
<thead>
<tr>
<th>Właściciel</th>
<th>Grupa</th>
<th>Rada</th>
</tr>
</thead>
</table>
{% else %}
<p>Brak głosowań!</p>
{% endif %}
{% endblock %}
,输出数据如下:
[{"user": 2, "group": 1, "council": 1}, {"user": 2, "group": 2, "council": 1}, {"user": 3, "group": 1, "council": 1}, {"user": 2, "group": 1, "council": 1}, {"user": 2, "group": 2, "council": 2}, {"user": 1, "group": 1, "council": 2}, {"user": 3, "group": 1, "council": 1}, {"user": 2, "group": 1, "council": 1}, {"user": 1, "group": 1, "council": 2}, {"user": 1, "group": 2, "council": 1}, {"user": 1, "group": 1, "council": 1}, {"user": 1, "group": 1, "council": 1}]
我的第二个问题是关于其他问题: 我将很多信息存储为选择:
My second question is about something else: I'm storing lot of information as choices:
STATUS_INACTIVE = 0
STATUS_ACTIVE = 1
STATUS_FINISHED = 2
STATUS_CHOICES = (
(STATUS_INACTIVE, 'Inactive'),
(STATUS_ACTIVE, 'Active'),
(STATUS_FINISHED, 'Finished'),
)
如何将数字而不是人类可读的值(无效")传递给上面的JSON?
How to pass not numbers but this human readable values ('Inactive') to JSON above?
对于第一个问题,请尝试在</thead>
标记后添加<tbody></tbody>
.重新运行代码.
For 1st question, try adding <tbody></tbody>
after </thead>
tag. Rerun the code.
要使DataTables能够增强HTML表,该表必须是有效且格式正确的HTML,并带有标头(thead)和单个正文(tbody).
还有另一种更简单的方法来呈现数据表.
views.py-
There is another simpler way to render datatable.
views.py -
context['json_data'] = votes # no need to use json.dumps
在html-
In html-
<table id="votes_list" class="display", style="width:100%">
<thead>
<tr>
<th>Właściciel</th>
<th>Grupa</th>
<th>Rada</th>
</tr>
</thead>
<tbody>
{% for data in json_data %}
<tr>{{ data.user }}</tr>
<tr>{{ data.group }} </tr>
<tr>{{ data.council }} </tr>
{% endfor %}
</tbody>
</table>
<script type="text/javascript">
$(document).ready(function() {
$('#votes_list').DataTable();
}
</script>
第二个问题-
{% if data.user == 1 %}
Active
{% elif data.user == 2%}
Inactive
{% else %}
Finished
{% endif %}
OR
{% if data.group == 1 %}
{{ status_dict.0 }}
{% elif data.group == 2%}
{{ status_dict.1 }}
{% else %}
{{ status_dict.2 }}
{% endif %}
>>>status_dict = dict(STATUS_CHOICES)
{0: 'Inactive', 1: 'Active', 2: 'Finished'}
在数据表中-您可以应用相同的逻辑.例如-
In datatable - you can apply the same logic. for example-
"columns": [
{ "data": "engine" },
{ "data": "browser" },
{
"data": "platform",
"render": function(data, type, row, meta) {
if(true)
return "display this"
};
return "false"
}
]