我遇到问题,我在调用方法post时试图更改我的jinja变量:
模板
<button id="button1">test</button>
<div class="myclass">
{{ hosts.0.name }} <--- NEED TO CHANGE THIS VARIABLE
</div
<script>
$('button1).click(function () {
var csrftoken = jQuery("[name=csrfmiddlewaretoken]").val();
$.ajax({
type:"POST",
url: "myurl/",
data: {
gfilter: "yes",
csrfmiddlewaretoken: csrftoken
},
success: function(){
console.log("success");
}
});
return false;
});
</script>
查看
def templates(request):
data = {}
data['hosts'] = zapi.host_get() #get my initial dates
try:
if request.POST['gfilter'] == "yes": #<-- get dates after push button
data['hosts'] = [{'name': 'test','host':'abc'}]
except:
print("except")
print(data['hosts'])
return render(request, 'zabbix/Templates.html', data)
鉴于我想在按下按钮时获取新数据, 在视图中,我确实看到了数据,但是变量{{hosts.0.name}}的值在模板中未更改,谁知道该问题如何解决,让我清楚了如何
谢谢您的期待