【发布时间】:2021-01-18 02:42:07
【问题描述】:
我有一个 django 视图,我希望上下文中的一些数据每秒更改几次。我可以在发送响应后将新上下文加载到模板中而不发送新请求吗?谢谢。
视图
def debug(request):
return render(request, 'all/debug.html', context={"x": x, "y": y})
模板
<div class="container">
<canvas id="data"></canvas>
<div class="graphing">
<select class="graph-select"></select>
<canvas id="graph" width="500"></canvas>
</div>
</div>
<script>
let y = "{{ y }}";
let x = "{{ x }}"
Chart.defaults.global.animation.duration = 0;
var ctx = document.getElementById('graph').getContext('2d');
var chart = new Chart(ctx, {
// The type of chart we want to create
type: 'line',
// The data for our dataset
data: {
labels: x.split` `.map(n => +n),
datasets: [{
label: 'My First dataset',
backgroundColor: 'rgb(255, 99, 132)',
borderColor: 'rgb(255, 99, 132)',
data: y.split` `.map(n => +n)
}]
},
// Configuration options go here
options: {
responsive: false,
}
});
</script>
我想要的是在不发送新请求的情况下动态更改 x 和 y 数据
【问题讨论】:
-
this 回答你的问题了吗?
-
@Sashaank 是的,确实如此。我最终(经过比预期更多的工作)能够让它发挥作用。谢谢!