django模板中javascript想使用views传过来的变量,然后对其排序,最后在输出显示,
遇到的问题是 不知道如何在javascript方法中获取views传过来的变量
#views代码
def index(request):
zblist=zhibiao.objects.all()
context={"zhibiaolist": zblist}
return render(request, "myapp/users/index.html", context)
下面是模板代码
html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport"
content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>首页title>
<script>
function sort_by(thid,dic_list){
th=document.getElementById(thid)
if (th.value=='') {th.value='pk';}
else if (th.value=='pk'){th.value='-pk';}
else {th.value='pk';}
dic_list.objects.all().order_by(th.value,th.name);
}
script>
head>
<body>
<center>
<span>信息浏览span>
<a href="{% url 'inquiry' %}">查询信息a><br/><br/>
<hr/>
<h3>基本情况h3>
<table width="800" border="1">
<tr>
<th id="th1" name="月份" value="" onclick="sort_by('th1',zhibiaolist)">月份th>
<th id="th2" name="人数" value="" onclick="sort_by('th2',zhibiaolist)">人数th>
<th id="th3" name="工作量" value="" onclick="sort_by('th3',zhibiaolist)">工作量th>
<th id="th4" name="占比" value="" onclick="sort_by('th4',zhibiaolist)">占比th>
tr>
{% load mytags %}
{% for zb in zhibiaolist %}
<tr>
<td>{{zb.月份}}td>
<td>{{zb.人数}}td>
<td>{{zb.工作量}}td>
<td>{{zb.占比|percentage}}td>
tr>
{% endfor %}
table>
center>
body>
html>
和在html上一样呀:
<script>
var context = {% zhibiaolist %}
function sort_by(thid,dic_list){
th=document.getElementById(thid)
if (th.value=='') {th.value='pk';}
else if (th.value=='pk'){th.value='-pk';}
else {th.value='pk';}
dic_list.objects.all().order_by(th.value,th.name);
}
</script>