Ajax 单击按钮时从 Django 1.9 代码获取新值
Ajax get new values from Django 1.9 code on button click
我正在尝试编写一个代码,当用户想要检查时,服务器(AWS 实例)的状态可以 returned 到浏览器 status.Sometimes 服务器可能需要一段时间启动,所以我想在每次用户单击按钮时刷新状态值(运行 Django 代码再次获取新值)。
示例:
[检查状态](按钮)
WebServer-Test 运行ning(每次单击按钮时都会刷新此行)
截至目前,我不确定如何将 view.py 再次变为 运行 以及 return html 页面要使用的新值。
这是我的view.py
def indcheckstatus(request, question_id, check_id):
inst() #runs an update on the instance (instance2[0].update() etc.)
text1 = 'WebServer-Test %s' % instance1[0].state
text2 = 'Gluster %s' % instance2[0].state
text3 = 'Gluster2 %s' % instance3[0].state
text4 = 'Database %s' % instance4[0].state
txt1 = 'WebServer-Test'
txt2 = 'Gluster'
txt3 = 'Gluster2'
txt4 = 'Database-Test'
if check_id == '1':
return render(request, 'awsservers/indcheckstatus.html', {'indstatus': text1, 'indserver':txt1})
elif check_id == '2':
return render(request, 'awsservers/indcheckstatus.html', {'indstatus': text2, 'indserver':txt2})
elif check_id == '3':
return render(request, 'awsservers/indcheckstatus.html', {'indstatus': text3, 'indserver':txt3})
elif check_id == '4':
return render(request, 'awsservers/indcheckstatus.html', {'indstatus': text4, 'indserver':txt4})
这是我的ajax代码+html代码
<html>
<body>
<title>Server Management</title>
<script src="http://code.jquery.com/jquery-latest.min.js" type="text/javascript"></script>
<script type="text/javascript">
$(document).ready(function() {
$("#state").hide()
var clicking = 1
$("#button").click(function() {
$.ajax({
url : '',
success : function(response) {
clicking++
$('#result').html($('#state').html() + clicking);
}
});
return false;
});
});
</script>
<noscript>
Some features require Javascript enabled
</noscript>
<form method="post">
{%csrf_token%}
<input id="button" type="button" value="Check Status"></input>
</form>
<div id="result">
{{ indserver }}
</div>
<div id="state">
{{ indstatus }}
</div>
<h2>Status of {{ indserver }}</h2>
<p>{{ indstatus }}</p>
<a href="{% url 'awsservers:index' %}">Go Back</a>
</body>
</html>
重点是我不想刷新整个页面来加载新值,但我希望在单击按钮时刷新该值。
我是 Django 的新手,尤其是 Ajax 所以如果我的代码不是很好请原谅我 "proper" :) 我非常感谢您的帮助!如果还有更好的方法从 Django 加载新值,请告诉我!
所以我找到了解决办法。
这是我更新的views.py
def indcheckstatus(request, question_id, check_id):
inst() // does an update of all instances
text1 = 'WebServer-Test %s' % instance1[0].state
text2 = 'Gluster %s' % instance2[0].state
text3 = 'Gluster2 %s' % instance3[0].state
text4 = 'Database-Test %s' % instance4[0].state
txt1 = 'WebServer-Test'
txt2 = 'Gluster'
txt3 = 'Gluster2'
txt4 = 'Database-Test'
if request.is_ajax(): // what to do if ajax code calls function
if check_id == '1':
return HttpResponse(text1) // return updated status back to html
elif check_id == '2':
return HttpResponse(text2)
elif check_id == '3':
return HttpResponse(text3)
elif check_id == '4':
return HttpResponse(text4)
else:
if check_id == '1':
return render(request, 'awsservers/indcheckstatus.html', {'indstatus': text1, 'indserver':txt1})
elif check_id == '2':
return render(request, 'awsservers/indcheckstatus.html', {'indstatus': text2, 'indserver':txt2})
elif check_id == '3':
return render(request, 'awsservers/indcheckstatus.html', {'indstatus': text3, 'indserver':txt3})
elif check_id == '4':
return render(request, 'awsservers/indcheckstatus.html', {'indstatus': text4, 'indserver':txt4})
这是我的 html + ajax 代码:
<html>
<body>
<title>Server Management</title>
<p>{{ indserver }} is starting up. </p>
<p>Click the button below to get the status of the server</p>
<script src="http://code.jquery.com/jquery-latest.min.js" type="text/javascript"></script>
<script type="text/javascript">
$(document).ready(function() {
$("#state").hide()
$("#start_id").hide()
$("#button").click(function() {
if ($("#start_id:contains('1')").length) { // to check which server was picked
$.ajax({
type: "get", // requesting updated values from django
url : '/menu/2/1/status-ind', // determines which function is called
success : function(response) {
($('#result').html(response));
} // response is basically "[ServerName ServerState]" form (seen from code above)
});
}
else if ($("#start_id:contains('2')").length) {
...... // do code
}
return false;
});
});
</script>
<noscript>
Some features require Javascript enabled
</noscript>
<form method="post">
{%csrf_token%}
<input id="button" type="button" value="Check Status"></input>
</form>
<div id="result">
{{ indserver }}
</div>
<div id="state">
{{ indstatus }}
</div>
<div id="start_id">
{{ start_id }}
</div>
<br>
<a href="{% url 'awsservers:index' %}">Go Back</a>
</body>
</html>
我在我的代码中添加了注释来解释我所做的更改。我意识到我必须编写与获取更新值所需的函数关联的 url,并从那里在我的 views.py 上写入正确的响应以用于 ajax 代码。它现在完美运行!
我正在尝试编写一个代码,当用户想要检查时,服务器(AWS 实例)的状态可以 returned 到浏览器 status.Sometimes 服务器可能需要一段时间启动,所以我想在每次用户单击按钮时刷新状态值(运行 Django 代码再次获取新值)。
示例:
[检查状态](按钮)
WebServer-Test 运行ning(每次单击按钮时都会刷新此行)
截至目前,我不确定如何将 view.py 再次变为 运行 以及 return html 页面要使用的新值。
这是我的view.py
def indcheckstatus(request, question_id, check_id):
inst() #runs an update on the instance (instance2[0].update() etc.)
text1 = 'WebServer-Test %s' % instance1[0].state
text2 = 'Gluster %s' % instance2[0].state
text3 = 'Gluster2 %s' % instance3[0].state
text4 = 'Database %s' % instance4[0].state
txt1 = 'WebServer-Test'
txt2 = 'Gluster'
txt3 = 'Gluster2'
txt4 = 'Database-Test'
if check_id == '1':
return render(request, 'awsservers/indcheckstatus.html', {'indstatus': text1, 'indserver':txt1})
elif check_id == '2':
return render(request, 'awsservers/indcheckstatus.html', {'indstatus': text2, 'indserver':txt2})
elif check_id == '3':
return render(request, 'awsservers/indcheckstatus.html', {'indstatus': text3, 'indserver':txt3})
elif check_id == '4':
return render(request, 'awsservers/indcheckstatus.html', {'indstatus': text4, 'indserver':txt4})
这是我的ajax代码+html代码
<html>
<body>
<title>Server Management</title>
<script src="http://code.jquery.com/jquery-latest.min.js" type="text/javascript"></script>
<script type="text/javascript">
$(document).ready(function() {
$("#state").hide()
var clicking = 1
$("#button").click(function() {
$.ajax({
url : '',
success : function(response) {
clicking++
$('#result').html($('#state').html() + clicking);
}
});
return false;
});
});
</script>
<noscript>
Some features require Javascript enabled
</noscript>
<form method="post">
{%csrf_token%}
<input id="button" type="button" value="Check Status"></input>
</form>
<div id="result">
{{ indserver }}
</div>
<div id="state">
{{ indstatus }}
</div>
<h2>Status of {{ indserver }}</h2>
<p>{{ indstatus }}</p>
<a href="{% url 'awsservers:index' %}">Go Back</a>
</body>
</html>
重点是我不想刷新整个页面来加载新值,但我希望在单击按钮时刷新该值。
我是 Django 的新手,尤其是 Ajax 所以如果我的代码不是很好请原谅我 "proper" :) 我非常感谢您的帮助!如果还有更好的方法从 Django 加载新值,请告诉我!
所以我找到了解决办法。
这是我更新的views.py
def indcheckstatus(request, question_id, check_id):
inst() // does an update of all instances
text1 = 'WebServer-Test %s' % instance1[0].state
text2 = 'Gluster %s' % instance2[0].state
text3 = 'Gluster2 %s' % instance3[0].state
text4 = 'Database-Test %s' % instance4[0].state
txt1 = 'WebServer-Test'
txt2 = 'Gluster'
txt3 = 'Gluster2'
txt4 = 'Database-Test'
if request.is_ajax(): // what to do if ajax code calls function
if check_id == '1':
return HttpResponse(text1) // return updated status back to html
elif check_id == '2':
return HttpResponse(text2)
elif check_id == '3':
return HttpResponse(text3)
elif check_id == '4':
return HttpResponse(text4)
else:
if check_id == '1':
return render(request, 'awsservers/indcheckstatus.html', {'indstatus': text1, 'indserver':txt1})
elif check_id == '2':
return render(request, 'awsservers/indcheckstatus.html', {'indstatus': text2, 'indserver':txt2})
elif check_id == '3':
return render(request, 'awsservers/indcheckstatus.html', {'indstatus': text3, 'indserver':txt3})
elif check_id == '4':
return render(request, 'awsservers/indcheckstatus.html', {'indstatus': text4, 'indserver':txt4})
这是我的 html + ajax 代码:
<html>
<body>
<title>Server Management</title>
<p>{{ indserver }} is starting up. </p>
<p>Click the button below to get the status of the server</p>
<script src="http://code.jquery.com/jquery-latest.min.js" type="text/javascript"></script>
<script type="text/javascript">
$(document).ready(function() {
$("#state").hide()
$("#start_id").hide()
$("#button").click(function() {
if ($("#start_id:contains('1')").length) { // to check which server was picked
$.ajax({
type: "get", // requesting updated values from django
url : '/menu/2/1/status-ind', // determines which function is called
success : function(response) {
($('#result').html(response));
} // response is basically "[ServerName ServerState]" form (seen from code above)
});
}
else if ($("#start_id:contains('2')").length) {
...... // do code
}
return false;
});
});
</script>
<noscript>
Some features require Javascript enabled
</noscript>
<form method="post">
{%csrf_token%}
<input id="button" type="button" value="Check Status"></input>
</form>
<div id="result">
{{ indserver }}
</div>
<div id="state">
{{ indstatus }}
</div>
<div id="start_id">
{{ start_id }}
</div>
<br>
<a href="{% url 'awsservers:index' %}">Go Back</a>
</body>
</html>
我在我的代码中添加了注释来解释我所做的更改。我意识到我必须编写与获取更新值所需的函数关联的 url,并从那里在我的 views.py 上写入正确的响应以用于 ajax 代码。它现在完美运行!